Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed delete first line from files

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 & --seperateoption 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

like image 872
ola Avatar asked Dec 09 '17 20:12

ola


People also ask

How do I remove the first N line from a file?

Using the tac and the sed Commands However, if we can reverse the order of lines in the input file, the problem will turn into “remove first n lines from a file.” A straightforward sed one-liner sed '1,n d' can remove the top n lines. After that, if we reverse the lines again, our problem gets solved.

How do you remove one line from a file in Linux?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I remove a line from a text file in Unix?

Note: In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines. However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command.

How do you remove the first line in awk?

The following `awk` command uses the '-F' option and NR and NF to print the book names after skipping the first book. The '-F' option is used to separate the content of the file base on \t. NR is used to skip the first line, and NF is used to print the first column only.


1 Answers

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
like image 185
janos Avatar answered Sep 30 '22 21:09

janos