Ubuntu – command to remove specific string from multiple files

command linetext processing

I have a directory dash7/ which contains multiple text files and I want to remove all lines containing the string D PRINT from all those files.

How can I do that easily from the command-line?

Best Answer

You can achieve this rather easily with sed which can happily look into multiple files

sed '/D PRINT/d' dash7/*
  • /D PRINT/ find a line with D PRINT
  • d delete the line
  • dash7/* look in all the files in the directory dash7 (add the path to it, for example ~/dash7 if required)

To actually change the files rather than print the edited text in the terminal, you need to add the -i flag to modify in place

sed -i '/D PRINT/d' dash7/*