Difference between revisions of "Tricks in bash"

From Computational Biophysics and Materials Science Group
Jump to: navigation, search
(Created page with "==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 [http://www.grymoire.com/unix/...")
 
m
Line 17: Line 17:
  
 
Basically, the command will be (I personally prefer the awk version):
 
Basically, the command will be (I personally prefer the awk version):
  awk ' {print;} NR % 40 == 0 { print ""; }' data
+
  awk ' {print;} NR % 40 == 0 { print ""; }' old.dat > new.dat
 
Modify the number and string inside "" accordingly.
 
Modify the number and string inside "" accordingly.
  

Revision as of 03:23, 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 'TCL: '| grep -v "Running" | sed 's/TCL: //g'

What the above will do: grep only lines containing "TCL: ", then grep lines without "Running", then replace "TCL: " with ""(nothing)

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.

vi editor

Insert a word/text in the beginning of each line

Again it is another discussion at StackExchange.

Commands:

:%s/^/foo: /