I want to remove the first line of all files in a folder if the file starts with uuid and so I have a working sed
command looking like this:
$ sed -i '' '/^uuid/d' *
which works fine and removes all lines starting with uuid.
Now I want to improve the script by only removing the first line if it starts with uuid since some of the files has multiple uuid:s and only the one on the first line should be deleted. So now I improved the command to look like this:
$ sed -i '' '1{/^uuid/d;}' *
Now this command also works but only on the first file in the folder and even if I run a simple (just remove first line) version like:
$ sed -i '' '1d' *
it still only affects the first file.
Why is this?
I'm on Mac (so the BSD version of sed as I've come to understand) and I also tried installing the gnu-sed version via Brew, $ brew install gnu-sed --with-default-names
, with no luck.
I've read sed - 25 examples to delete a line or pattern in a file, sed - 20 examples to remove / delete characters from a file and googled sed delete first line in files
UPDATE 1: As proposed in the comments by john1024 I've tested with the -s
option but not sure how to use it.
$ sed -s '1d' ./*
sed: illegal option -- s
When I check man sed
I can find the -s & --seperate
option so I must do something wrong here.
UPDATE 2: Ok, progress ... find . -iname '*.yml' -exec sed -i '' -e '1{/uuid/d;}' {} \;
does the trick but I get error message saying sed: can't read : No such file or directory
Thanks for any help or guidance!
:ola
sed -i '' '1{/^uuid/d;}' *
will modify only the first file,
because the line numbers are counted cumulatively across files,
so "line 1" occurs only once, it's the first line of the first file.
To do something with the first line of multiple files,
you need to run sed
once per file.
You can do that using a simple for
loop:
for f in *; do sed -i '' '1{/^uuid/d;}' "$f"; done