Ubuntu – How to edit multiple text files with a single command

command linetext processing

I want to remove the first line of multiple text files and replace it with another line. Is there a way to do this using the terminal?

I know how to use vim, is there a way to automate it with it? I want to avoid doing it manually (as in editing each file individually).

If possible I'd like to avoid installing new libraries (especially for something like that which i consider very simple – but I could be wrong).

Thanks in advance!

Best Answer

You can use sed :

sed -i.bak '1s/^.*$/new line/' *.txt

This will replace the first line of all .txt files in the current directory with the text new line, change it to meet your need.

Also the original files will be backed up with .bak extension, if you don't want that just use :

sed -i '1s/^.*$/new line/' *.txt