Tricks in bash

From Computational Biophysics and Materials Science Group
Revision as of 18:03, 15 May 2014 by Kevin (Talk | contribs) (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/...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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 ""; }' data

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: /