Understanding !d command in sed with respect to saves

Posted by richardh on Super User See other posts from Super User or by richardh
Published on 2012-06-18T15:03:38Z Indexed on 2012/06/18 15:18 UTC
Read the original article Hit count: 279

Filed under:

I have a directory of tab-delimited text files and some have comments in the first few lines that I would like to delete. I know that the first good line starts with "Mark" so I can use /^Mark/,$!d to delete these comments. After this deletion I have several other replacements that I make in the (new) first line that has variable names.

My question is, why do I have to save sed's output to get my script to work? I understand that if I line is deleted, then the output doesn't proceed downstream because there is no output. But if I don't delete (i.e., !d) then why do I have to save to file? Thanks!

Here is my shell script. (I'm a sed newbie, so any other feedback is also appreciated.)

#!/bin/bash
for file in *.txt; do
    mv $file $file.old1
    sed -e '/^Mark/,$!d' $file.old1 > $file.old2
    sed -e '1s/\([Ss]\)hareholder/\1hrhldr/g'\
        -e '1s/\([Ii]\)mmediate/\1mmdt/g'\
        -e '1s/\([Nn]\)umber/\1o/g'\
        -e '1s/\([Cc]\)ompany/\1o/g'\
        -e '1s/\([Ii]\)nformation/\1nfo/g'\
        -e '1s/\([Pp]\)ercentage/\1ct/g'\
        -e '1s/\([Dd]\)omestic/\1om/g'\
        -e '1s/\([Gg]\)lobal/\1lbl/g'\
        -e '1s/\([Cc]\)ountry/\1ntry/g'\
        -e '1s/\([Ss]\)ource/\1rc/g'\
        -e '1s/\([Oo]\)wnership/\1wnrshp/g'\
        -e '1s/\([Uu]\)ltimate/\1ltmt/g'\
        -e '1s/\([Ii]\)ncorporation/\1ncorp/g'\
        -e '1s/\([Tt]\)otal/\1ot/g'\
        -e '1s/\([Dd]\)irect/\1ir/g'\
        $file.old2 > $file
        rm -f $file.old*
done

© Super User or respective owner

Related posts about sed