Friday, April 22, 2011

How to print after matching a pattern using awk

cat file_name | awk '{ if ( $0 ~/pattern_to_match/ ) { pstart = 1 }; if (pstart) { print }

Shell script which runs the command in a directory on a list of files:

cd $DIRNAME
mkdir -p orig
for name in `ls`; do
cat $name | awk '{ if ( $0 ~/pattern_to_match/ ) { pstart = 1 }; if (pstart) { print } }' > $name.tmp
mv $name ./orig
mv $name.tmp $name
done

0 Comments:

Post a Comment

<< Home