Difference between revisions of "Tricks in bash"

From Computational Biophysics and Materials Science Group
Jump to: navigation, search
m
m
Line 10: Line 10:
 
===Delete some elements in your lines===
 
===Delete some elements in your lines===
 
Commands:
 
Commands:
  cat data | grep 'TCL: '| grep -v "Running" | sed 's/TCL: //g'
+
  cat data | grep 'ABC'| grep -v "XYZ" | sed 's/ABC/DEF/g'
'''''What the above will do:''' grep only lines containing "TCL: ", then grep lines without "Running", then replace "TCL: " with ""(nothing)''
+
'''''What the above will do:''' grep only lines containing "ABC", then grep lines without "XYZ", then replace "ABC" with "DEF''
  
 
===Insert a new line after every n lines===
 
===Insert a new line after every n lines===

Revision as of 03:25, 17 May 2014

sed/awk

These two commands are so powerful that you must learn especially when you are dealing with data file with millions lines.

Here is a very detail introduction on sed, you may not have to read it thoroughly but it covers most of the situation that you may encounter.

Grep only the first n columns

Commands:

cat data | awk '{print $1 "\t" $2 "\t" $3}' > result.dat

Delete some elements in your lines

Commands:

cat data | grep 'ABC'| grep -v "XYZ" | sed 's/ABC/DEF/g'

What the above will do: grep only lines containing "ABC", then grep lines without "XYZ", then replace "ABC" with "DEF

Insert a new line after every n lines

Here is an accurate answer to the question on StackExchange, I found the situation often to encounter but the solution is surprisingly difficult to find.

Basically, the command will be (I personally prefer the awk version):

awk ' {print;} NR % 40 == 0 { print ""; }' old.dat > new.dat

Modify the number and string inside "" accordingly. Remember to redirect to a new datafile, otherwise it will only print out on the screen.

vi editor

Insert a word/text in the beginning of each line

Again it is another discussion at StackExchange.

Commands:

:%s/^/foo: /