Today I needed to massively clean lines containing x’s in front some css declarations in a group of files (context: this is some sort of trick I frequently use to cancel instead of deleting a property in a declaration while live editing css with the indispensable webdeveloper plugin for Firefox. practical as it the trick, if you forget to erase it immediately the x yields error on the www3 validation later on)
I could easily filter up the desired files and even remove the line doing something like this:
ls | awk '/.css$/' | xargs sed -i.bk -e 's/^ *x.*$//g'
The problem was that I did not want to simply erase the line without it’s corresponding line break, as will be the result here (the explanation is that sed by default, operates on single lines, stripping the line break from the stdin and appending it back after doing the substitution)
The simple way I found around it was to filter all lines matching the regex (outputing the rest to the file) instead of performing the substitution on every single line which would leave and empty line.
Like this:
ls | awk '/.css$/' | xargs sed -i.bk -e '/^ *x.*$/d'
I figured how to discard lines from this excellent collection of sed oneliners It shows two alternatives.
# print only lines which do NOT match regexp (emulates “grep -v”)
sed -n ‘/regexp/!p’ # method 1, corresponds to above
sed ‘/regexp/d’ # method 2, simpler syntax
Here’s the complete online command used at the shell command line (which I ran in emacs by the way)
find . -type f -exec grep -i '^ *x' /dev/null {} + | awk '!/svn|htdocs/' | cut -c 3- | awk '!/^#/' | awk -F ':' '{print $1}' | awk '/.css$/'| xargs sed -i.bk -e '/^ *x.*$/d\
'