Sundeep
  • Member for 6 years, 8 months
  • Last seen this week
2 answers
0 votes
100 views
How can I group by the country in column two and then sort by the resulting sum?
2 votes

With GNU awk: $ awk 'BEGIN{PROCINFO["sorted_in"] = "@val_num_desc"} {a[$2]+=$1} END{for(k in a) print a[k], k}' ip.txt 8 UA 6 US 2 GB See gawk manual: Using Predefined ...

View answer
4 answers
1 votes
52 views
Replace (one or) two different patterns in a file with regexp
Accepted answer
2 votes

With lookarounds, you can check if the strings around | are same or not. For example: $ cat ip.txt [[foo>a|d]] [[foo>b|e]] [[foo>c|f]] # same as: rg -NP '\[\[([^>]+)>([^|]+)\|(?!\2])([...

View answer
1 answers
0 votes
43 views
sed to insert all necessary bash escapes into a filepath
Accepted answer
2 votes

printf '%q\n' 'My Books/Pride & Prejudice.pdf' will give your desired output. You can use a glob pattern, for example, printf '%q\n' * to get the results for multiple files. Depending on your use ...

View answer
4 answers
0 votes
128 views
How to replace every digit after special character '$' with '*'
2 votes

With perl $ perl -pe 's/\$\K\d+/$&=~s|.|*|gr/ge' ip.txt axbnx $** ghjas $**$ axbnx $*** ghjas $ axbnx $*** ghjas $$*** \$\K\d+ match digits preceded by $ character (\K will prevent $ from being ...

View answer
2 answers
0 votes
220 views
awk gensub regex matching group between square brackets
2 votes

$ s='-0.005 Tc 0.005 Tw [(T)-8.5(o)-3.2(p)-15.3(ik)]TJ' $ # if you want to delete []TJ $ echo "$s" | awk '{print gensub(/\[([^]]+)]TJ/, "\\1", "g")}' -0.005 Tc 0.005 Tw (...

View answer
2 answers
0 votes
61 views
Split Column into multiple column when there is blank column
2 votes

$ csplit -zsf file ip.txt /^$/+1 {*} $ paste -d, file* | head -n -1 A,1,Q B,2,W C,,A From manual: csplit - split a file into sections determined by context lines -z, --elide-empty-files remove empty ...

View answer
8 answers
4 votes
590 views
Count the number of rows with a string occurring n times in multiple columns
2 votes

With GNU grep or ripgrep $ LC_ALL=C grep -c $'\t''0\b.*\b0\b.*\b0\b' ip.txt 7 $ rg -c '\t0\b.*\b0\b.*\b0\b' ip.txt 7 where $'\t' will match tab character, thus working even if first column is 0. ...

View answer
5 answers
1 votes
2k views
awk: extract tab-delimited columns matching sub-strings in first line
2 votes

With perl $ perl -F'\t' -lane 'if(!$#ARGV){ $h{$_}=1 } else{ @i = grep { $h{$F[$_]} == 1 } 0..$#F if !$c++; print join "\t", @F[@i]}' strings.txt ...

View answer
1 answers
0 votes
23 views
delete rows with duplications in first column in bash
Accepted answer
2 votes

You can use awk here: $ awk -F'\t' 'NR==1 || !seen[$1]++' ip.txt Symbol ATCACGACAGACAGGT.1 ATCACGACAGCCTATA.1 TTTGTCATCATGTCTT.1 STPG1 0 1 3 NFYA 0 0 1 ABD ...

View answer
1 answers
1 votes
66 views
The "perfect" egrep in a file with a lot of garbage and one huge line only
Accepted answer
2 votes

Try grep -o 'abc[a-z0-9]*\.bmp' [a-z0-9]* this will match zero or more of lowercase alphabets and digits \. this will match a . character literally Why ^abc*+.bmp doesn't work ^ is start of line ...

View answer
4 answers
1 votes
11k views
grep range of numbers in a text file
2 votes

unquoted strings are interpreted by the shell before command is executed, in your case, the command you tried would be expanded to grep 20180619 20180620 20180621 test $ echo grep 201806{19..21} test ...

View answer
5 answers
6 votes
8k views
sed with two conditions
2 votes

How about humble grep $ grep -v '^nnn.\{23\}' ip.txt nnnASDDGfdgdsfn sadbSADSDDFSDFrrrRRRRRRRttTGGGG $ # if ERE is available $ grep -vE '^nnn.{23}' ip.txt nnnASDDGfdgdsfn ...

View answer
5 answers
1 votes
3k views
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
3 answers
0 votes
5k views
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
8 answers
5 votes
4k views
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
5 answers
29 votes
24k views
Find intersection of lines in two files
2 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
3 answers
1 votes
114 views
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
3 answers
1 votes
5k views
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
3 answers
12 votes
40k views
How to use sed to replace numbers with parenthese?
2 votes

Assuming input has : separated text and 2nd column needs to be changed: $ awk 'BEGIN{ FS=OFS=":" } {$2 = "("$2", 0)"} 1' ip.txt hello:(123, 0): world hello:(783, 0): world hello:(479, 0): world Or, ...

View answer
4 answers
9 votes
7k views
Replace all occurrence of character except first one
2 votes

$ cat ip.txt test1,test2,test3 test4,test5 test6,test7,test8,test9,test10 test11,test12,test13,test14 test15 $ perl -F, -ane 'print "$F[0]"; print ",".join(";",@F[1..$#F]) if($#F > 0)' ip.txt ...

View answer
10 answers
17 votes
9k views
Remove newline character just every N lines
2 votes

Generic solution, replace 5 with number of lines required $ # eof to ensure last line has newline ending $ seq 16 | perl -pe 's/\n/ / if ++$i%5 && !eof' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

View answer
2 answers
0 votes
133 views
Select lines of file 1 where field of file 2 > x
2 votes

With awk and getline (see all about getline for caveats) $ # can also use: awk '{getline num < "file2"} num>0.5' file1 $ awk -v cmp_f='file2' '{getline num < cmp_f} num>0.5' file1 ...

View answer
3 answers
3 votes
5k views
Delete lines x after a match
2 votes

For generic cases, see this excellent answer from Ed Morton: Printing with sed or awk a line following a matching pattern . d) Print every record except the Nth record after some regexp: awk 'c&...

View answer
4 answers
3 votes
289 views
Replace line M in file1 with line N in file2
2 votes

Try this: $ cat f1 foo bar xyz baz temp good $ cat f2 1 2 3 4 5 6 $ awk -v m=3 -v n=4 'NR==FNR{if(FNR==n) s=$0; next} FNR==m{$0=s} 1' f2 f1 foo bar 4 baz temp good NR==FNR will be true only when ...

View answer
2 answers
0 votes
66 views
Remove string using certain values (-)
Accepted answer
2 votes

$ cat ip.txt foo-11.11-fo.o-foo-bar bar-foo-11.11-22.11 $ rev ip.txt rab-oof-o.of-11.11-oof 11.22-11.11-oof-rab $ rev ip.txt | cut -d- -f3- | rev foo-11.11-fo.o bar-foo Reverse each line, then use ...

View answer
3 answers
2 votes
3k views
print the duplicate lines only on fields 1,2 from csv file
Accepted answer
2 votes

$ awk -F, 'NR==FNR{a[$1,$2]++; next} a[$1,$2]>1' file.txt file.txt spark2-thrift-sparkconf,spark.history.fs.logDirectory,{{spark_history_dir}} spark2-thrift-sparkconf,spark.history.fs.logDirectory,...

View answer
2 answers
0 votes
1k views
How to convert this timestamp format to another format in Perl?
Accepted answer
2 votes

$ perl -pe 's/\b(\d\d)(\d\d)(\d{4})-(\d\d)(\d\d)\+\d{4}\b/$3-$2-$1\x27T\x27$4:$5:00/g' ip.csv Ni, Aika, Aika_l Un, Unen, Unen_kesto, Uniluokat_R, Uniluokat_k, Uniluokat_s, HRV_RMSSD_a, HRV_RMSSD_i, ...

View answer
2 answers
0 votes
896 views
Getting egrep -o output in one line
Accepted answer
2 votes

Looks like you want to get all values after : and join them using space or tab perl is one option $ perl -lne 'print join "\t",/:\h*\K[^ ]+/g' output 44.75TB 0.44TB 42.04TB 6.07% /:\h*\K[^ ]+/g ...

View answer
1 answers
0 votes
497 views
sed to append at specific line within certain limit only
Accepted answer
2 votes

all characters after a command is treated as string to be appended, hence sed will complain about unmatched `{' One way is to use -e (tested with GNU sed, not sure about portability) $ sed -e '/...

View answer
2 answers
0 votes
548 views
renaming file in bash from an info file
2 votes

$ while read -r c1 c2 c3; do echo mv "$c3" "$c2"; done < info.txt mv ERR315336_1.fastq.gz placenta_6c_ERR315336_1.fastq.gz mv ERR315336_2.fastq.gz placenta_6c_ERR315336_2.fastq.gz mv ERR315337_1....

View answer
1
3 4
5
6 7
10