swisstech.net

tech and photography

Using awk to remove lines from a file

2013-03-14

A little goodie I’m writing up here to document for my own future reference.

Objective: Remove all lines matching a certrain pattern from all files in a directory structure. The pattern is in the form of “part1..part2”

First try: grep and sed, unfortunately that leaves me with blank lines where the matching content was and I want that line removed completely. Apparently sed can’t do that.

Second try: use awk with regexes. It took me a while and I wanted to use ’next’ on matching lines but that didn’t seem to work so I ended up printing the entire line if the regex didn’t match. Here goes:

1
2
3
4
5
6
7
8
# oneliner
for file in $(egrep -r "part1\.[a-Z]+\.part2" . | cut -d: -f1 | sort -u); do echo $file; awk '!(/part1/ && /part2/) { print $0 }' $file > tmp && mv tmp $file; done
 
# the same a little nicer
for file in $(egrep -r "part1\.[a-Z]+\.part2" . | cut -d: -f1 | sort -u); do
    echo $file
    awk '!(/part1/ && /part2/) { print $0 }' $file > tmp && mv tmp $file
done