Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove line of text from multiple files in Linux

Is there a simple way to remove the same line of text from a folder full of text documents at the command line?

like image 788
Robert Avatar asked Jul 25 '09 18:07

Robert


People also ask

How do I remove a 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 delete a range of lines in Linux?

The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.

How do I remove a prefix from multiple files in Linux?

multiple-files-remove-prefix.mdfor file in prefix*; do mv "$file" "${file#prefix}"; done; The for loop iterates over all files with the prefix. The do removes from all those files iterated over the prefix.


1 Answers

If your version of sed allows the -i.bak flag (edit in place):

sed -i.bak '/line of text/d' *  

If not, simply put it in a bash loop:

for file in *.txt do     sed '/line of text/d' "$file" > "$file".new_file.txt done 
like image 140
ennuikiller Avatar answered Sep 22 '22 20:09

ennuikiller