using awk to truncate a file based on string
One more awk trick
. Let’s say you have a huge file. You only want to get all the lines of the file which are:
- before certain string
- after certain string
The solutions:
seq 1 30 | awk '/.*8.*/ { nextfile } { print $0 }'seq 1 30 | awk '/.*8.*/ { pr = 1 } { if (pr == 1) print $0 }'seq 1 30 | awk '/.*8.*/ { pr = 1; print $0 } { if (pr == 1) print $0 }'
Short explanation: awk is instructed to look for regular expression (which is enclosed in ‘/’) and in (1) stops parsing the file before stopping each line is echoed as is. In (2) and (3) it sets pr variable to be 1 (it’s 0 by default and is short of “print”), and lines are echoed only if pr is 1.
Technorati Tags: awk, truncate file, cli, linux


Check out “man csplit” or “info coreutils csplit”
I downloaded coreutils source code from gnu.org and found many useful utilities that I’ve never heard of and seem to be very useful
Comment by Amit — October 24, 2007 @ 07:06