Sundeep
  • Member for 6 years, 9 months
  • Last seen this week
Convert all text from uppercase to lowercase and vice versa?
1 votes

ruby has a string method for that, similar usage from command line like perl $ echo 'qWeRtY' | ruby -pe '$_.swapcase!' QwErTy See also ruby-doc Encoding $ ruby -e 'puts Encoding.default_external' ...

View answer
Matching a pattern in Perl
1 votes

Not entirely sure, but this might help #!/usr/bin/perl use strict; use warnings; my $fname = "deep.txt"; open(my $fh, "<:encoding(ASCII)", $fname) || die "cannot open $fname for reading"; my $...

View answer
How to operate on all columns with datamash?
1 votes

I don't see an option to specify unknown range in datamash manual Try this perl one-liner $ perl -lane '$s[$_]+=$F[$_] for 0..$#F; END{print join " ", @s}' ip.txt 1332 1665 1998 -a option will auto ...

View answer
Using sed to edit a string over two lines
1 votes

If are okay with using awk instead of sed $ awk -F'[{ ]' 'c && c-- && $1=="macrob" && $2==s{sub(s" ", "")} $1=="macroa"{c=1; s=$2} 1' ip.txt macroa{abc def 123 ...

View answer
Reverse the order of first 3 digits in a line
5 votes

To modify OP's attempt $ cat ip.txt 214 4234-5555 foo 123 4533-3242 $ sed -r 's/([0-9])([0-9])([0-9])/\3\2\1/' ip.txt 412 4234-5555 foo 321 4533-3242 $ # adding parenthesis as well $ sed -r 's/([0-...

View answer
How to delete input field in AWK?
4 votes

Some alternatives 1) pre-process the input to remove the field first, easy to do with cut if field separator is single character $ s='field1,field2,field3' $ # use 'cut -d, -f1,3-' if --complement ...

View answer
Get last column of string without AWK
2 votes

With grep, assuming -o option is available (also, going by title that last column is needed, not just particular column) $ echo 'foo bar 123' | grep -o '[^ ]*$' 123 $ echo 'foo;bar;123' | grep -o '[^;...

View answer
extracting part of a string in a column and retain other columns
2 votes

$ # assuming `rs[digits]` string will match only in 2nd column $ # string matched within () will get printed $ perl -lne 'print /(rs\d+\t)[^\t]+\t([^\t]+)/' ip.txt rs199 info2 rs2778 info5 $ # to ...

View answer
Remove duplicate characters in bash
2 votes

Another sed solution: for single alphabetic character: $ echo 'here hear' | sed 's/\(\([[:alpha:]]\).*\)\2/\1' here ear for all of them, the g flag won't help as characters already processed won't ...

View answer
Find a string and replace another string after the first is found
1 votes

generic solution using awk, consider the following modified input file with multiple 1s and 4s $ cat ip.txt 1 foo 1 xyz 4 4 1 1 eeeee 4 1 rrrrrr 4 1 4 Use a flag to indicate that 1 was matched and a ...

View answer
Find intersection of lines in two files
3 votes

slightly different awk version and equivalent perl version time reported for three consecutive runs $ # just realized shuf -n2000000 -i1-2352452 can be used too ;) $ shuf -i1-2352452 | head -...

View answer
Using "sed", How do I add a value after the "=" sign in a string?
Accepted answer
1 votes

For given example, try sed -i "/^$given_str/{s/.//; s|.$|$value\"|}" "$file" Note the use of double quotes, allows variable interpolation /^$given_str/ if given string matches at start of line s/.//...

View answer
Inverting two lines in a group of 3 repeating
Accepted answer
2 votes

$ cat -n ip.txt 1 AAAAAAAAAA 2 BBBBBBBBBB 3 CCCCCCCC 4 5 DDDDDDDDDDD 6 EEEEEEEEEEE 7 FFFFFFFFFFF 8 9 GGGGGGGGGGG 10 HHHHHHHHHHH 11 ...

View answer
AWK issue using unix scripts
0 votes

Another way is to directly control the output record separator(ORS) $ awk -F'*' '$1=="AK3"{ORS=OFS; print $2, $3} $1=="AK4"{ORS=RS; print $4, $5}' ip.txt BPS 2 1 RMT 12 7 CR by default, output ...

View answer
grep: detect multi-line pattern with double capture
Accepted answer
3 votes

try this: $ cat ip.txt foo baz .LBB7_7: branch .LBB7_7 xyzyadsf .LBB8_3: mov a, b branch .LBB8_3 nop $ grep -zoP '(\.LBB\d_\d+):\s*branch\h+\1\n' ip.txt .LBB7_7: branch ....

View answer
How to count the number of characters in a line, except a specific character?
7 votes

assuming that count is needed for each line other than space character and N $ perl -lne 'print tr/N //c' ip.txt 1 1 1 0 1 2 2 return value of tr is how many characters were replaced c to ...

View answer
Using "find" non-recursively?
Accepted answer
61 votes

You can do that with -maxdepth option /bin/find /root -maxdepth 1 -name '*.csv' From man find -maxdepth levels Descend at most levels (a non-negative integer) levels of directories below the ...

View answer
Search for a string and delete till occurrence of next character
4 votes

$ echo "svn+ssh://test@svn-server.com/repo 2015/branches/DEV_BRANCH_21/main/code/main_input.c" | grep -oP 'branches/.*?/\K.*' main/code/main_input.c $ echo "svn+ssh://test@svn-server.com/repo-2015/...

View answer
Round down/truncate decimal places within column
Accepted answer
10 votes

easy enough with grep $ cat ip.txt 123.434 1456.8123 2536.577 345.95553 23643.1454 $ grep -o '^[0-9]*\.[0-9]' ip.txt 123.4 1456.8 2536.5 345.9 23643.1 ^ start of line [0-9]* zero or more ...

View answer
Replace a character only if preceded by a digit
4 votes

Use capture groups and back-reference $ echo 'abcxd,abcx,6000/600x1' | sed 's/\([0-9]\)x/\1*/g' abcxd,abcx,6000/600*1 \([0-9]\) capture this digit so that it can be referenced \1 See Capture Groups ...

View answer
Double quote value assignments stored in a CSV?
4 votes

With perl $ # unlike sed/awk, newline is still part of input record $ perl -pe 's/=\K[^,\n]*/"$&"/g' ip.txt dt="2016-06-30",path="path1",site="US",mobile="1" dt="2016-06-21",path="path2",site="...

View answer
check if string is present in a list and output a third file if string is present
1 votes

Another perl solution $ perl -lne 'if(!$#ARGV){ $h{$_}=1 } else{ s/\h\K\H+/$h{$&} ? 1 : 0/ge; print } ' mylist.tab data.tab Info_1 0 1 1 Info_2 1 0 ...

View answer
how to filter inner quotes?
Accepted answer
1 votes

With GNU sed, which supports replacing all occurrences from particular count $ echo label=\"123 \"456\" 789\" \"AB C\" e f gh | sed -E 's/"/\\"/2g; s/\\("[^"]*)$/\1/' label="123 \"456\" 789\" \"...

View answer
Replace lines matching a pattern with lines from another file in order
1 votes

With GNU sed, similar to awk+getline $ sed -e '/^!!/{R file2.txt' -e 'd}' file1.txt aaaaaa bbbbbb first line second line ccccc ddddd third line R would give a line one at a time order is important, ...

View answer
Pruning fields out of a file
0 votes

with sed $ echo 'a,,b,c,d' | sed -E 's/^(([^,]*,){2})[^,]*,/\1/' a,,c,d $ echo 'a,,b,c,d' | sed -E 's/^(([^,]*,){3})[^,]*,/\1/' a,,b,d [^,]*, zero or more non comma text followed by comma {2} or {3} ...

View answer
Replace "_" with "|" in a file on 5th field only
8 votes

With sed provided the 5th field has only one _ to be replaced $ sed -E 's/^(([^|]+\|){4}[^_]+)_/\1|/' ip.txt rat|minty|ruhul|balaji|rat|123|decode|rat_123|abc|def|ghi|jkl|rat|cde|ind|rat rat|minty|...

View answer
using awk to make new file, results in issues using 1 specific column, can't figure out what is wrong
Accepted answer
3 votes

with dos style line endings: $ cat -e ip.txt MT 200610-1 100 2757^M$ MT 200610-10 100 6753 ^M$ MT 200610-100 100 15173^M$ $ awk 'BEGIN{FS=="\...

View answer
Counting the number of lines having a number greater than 100
3 votes

Similar solution with perl $ seq 98 105 | perl -ne '$c++ if $_ > 100; END{print $c+0 ."\n"}' 5 Speed comparison: numbers reported for 3 consecutive runs Random file: $ perl -le 'print int(rand(...

View answer
How to print all fields containing one of two strings in a table with awk
2 votes

If perl solution is fine: $ cat ip.txt A dog999 dog284 cow284 pig383 pig234 cow432 chicken432 B cow394 cow432 cow345 dog983 pig345 chicken532 C dog847 pig357 pig236 ...

View answer
Execution cycle of sed s/REGEXP/REPLACEMENT/g
8 votes

The why of the question been already answered by @heemayl Workaround with sed (here assuming GNU sed) - loop recursively as long as a match is found $ echo ,,, | sed -E ':a s/(,)(,)/\1*\2/ ; ta' ,*,*...

View answer
1 2 3
4
5
10