Using sed to delete some text on multiple files in a directory

Viewed 189

I'm removing the same text from a number of files using

sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p'  myfile.emlx

which works so far.

I understand that I can run this over all the files in the current directory. I tested this using

sed -i ''  's/^/\t/'  *.txt

and that worked as expected, however when I try

sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' *.emlx

although the first file is correctly ammended and saved, all the text from all the other files is removed. I've tried various combinations of -i and -n etc but can't work out my problem.

1 Answers

The problem with your approach is that sed carries patterns across file boundaries. You will want to revert to running one sed instance per file.

for file in *.emlx; do
    sed -n -i '' '/<?xml version="1.0" encoding="UTF-8"?>/q;p' "$file"
done

Quick demo:

bash$ printf '%s\n' >foo "one" "two" "three"
bash$ printf '%s\n' >bar "four" "five" "six"
bash$ tail *
===> foo <===
one
two
three
===> bar <===
four
five
six
bash$ sed '/two/,/five/d' foo bar
one
six
Related