Difference between revisions of "Tricks in bash"
m |
m |
||
Line 6: | Line 6: | ||
===Grep only the first n columns=== | ===Grep only the first n columns=== | ||
Commands: | Commands: | ||
− | cat | + | cat old.dat | awk '{print $1 "\t" $2 "\t" $3}' > new.dat |
===Delete some elements in your lines=== | ===Delete some elements in your lines=== |
Revision as of 03:26, 17 May 2014
Contents
[hide]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 old.dat | awk '{print $1 "\t" $2 "\t" $3}' > new.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: /