Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sed only handle one file when receiving multiple files as input?

Tags:

macos

sed

I have six CSV files I want to manipulate with sed

sed -i -e "1d" 1.csv 2.csv 3.csv 4.csv 5.csv 6.csv

or

sed -i -e "1d" *csv

but when running any of these commands only 1.csv is modified. However, sed renames the original of 2-6.csv (the timestamp of the backup file remain unchanged) to 2.csv-e and so on and create a new file 2.csv but this new file is identical to 2.csv-e (and diff:ing 2.csv and 2.csv-e returns no differences) (diffing 1.csv and 1.csv-e lists some changes).

How do I make sed accept more than one file as input?

This is on OS X.

like image 920
d-b Avatar asked Jan 29 '23 07:01

d-b


2 Answers

The OSX sed man page says

Sed Addresses

An address is not required, but if specified must be a number (that counts input lines cumulatively across input files), a dollar (``$'') character that addresses the last line of input, or a context address (which consists of a regular expression preceded and followed by a delimiter).

So, you need a different approach

for file in 1.csv 2.csv 3.csv 4.csv 5.csv 6.csv; do
    sed -i -e "1d" "$file"
done
like image 76
glenn jackman Avatar answered Feb 06 '23 07:02

glenn jackman


With GNU-sed, you can use -s:

 sed -i.bak -se  "1d" {1..6}.csv 

(The {1..6} construct works at least in bash.)

The manpage of sed says:

   -s, --separate

          consider files as separate rather than as a single continuous long stream.

and it works for me, including the in-place command and generation of .bak files. But I don't know for MacOS. Try with testdata first or read the man page.

like image 44
user unknown Avatar answered Feb 06 '23 07:02

user unknown