sed and variable as target file

for file in `ls /tmp/*_sw_list`; do
  /usr/bin/sed -i '' '1,/^Software\ Update/d' $file
done

In my script, this doesn't work. I can copy-and-paste it, and it works. Enabling debugging shows it is resolving the file name correctly... it isn't an issue with special characters in the filename. It returns an error code of 0, but doesn't actually modify the file. Permissions on the file are 644.

This is a useless use of ls *.

Piping the * through ls accomplishes nothing you couldn't have accomplished without it, except possibly scrambling any filenames with whitespace in them -- backticks split across all whitespace, not newlines.

for FILE in /tmp/*_sw_list
do
        /usr/bin/sed -i '' '1,/^Software\ Update/d' "$FILE"
done

...or even:

/usr/bin/sed -i '' '1,/^Software\ Update/d' /tmp/*_sw_list

What does the '' do? I don't understand the meaning of the blank expression, but if you say it works...

Why are you escaping a space?

Above examples are for '*', to catch EVERYTHING. I'm going to wind up with an unknown number of files in /tmp/ called file_sw_list, file2_sw_list, etc. etc.

It would be a pretty freaky coincidence to wind up with filenames matching my pattern with whitespace... they're generated from unquotes variables. It could happen, and I always like to sanity check as much as possible, but that's pretty far down my list of worries.

Neither work. I get the same result.

Two single quotes, not a double quote. I'm performing the operation on a file, and don't want a "backup". BSD sed is picky about this stuff (OS is OSX).

Because there's a literal space in the pattern I'm matching?

In any case, it turned out to be a second pattern that matched '^Software\ Update' that was so close to the top I didn't notice just a couple of lines missing :slight_smile:

Look closely, my examples use * too. They are not pseudocode, * does actually work in those places. That's why putting it through ls is redundant.

If you're concerned about * matching too many files and using ls instead because of this, plugging too many files into ls does not make them not be too many files either.