Difference between revisions of "Tricks in bash"

From Computational Biophysics and Materials Science Group
Jump to: navigation, search
m
m
Line 18: Line 18:
 
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 ""; }' old.dat > new.dat
 
  awk ' {print;} NR % 40 == 0 { print ""; }' old.dat > new.dat
Modify the number and string inside "" accordingly.
+
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==
 
==vi editor==

Revision as of 03:24, 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. 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: /