Deleting Lines Containing Specific string from multiple files

Deleting Lines Containing Specific string from multiple files recursively

Syntax:

grep -Rl "STRING" /directory/path/ | xargs sed -i "/STRING/d"

Example:

Below example delete lines containing @license tag from src/main/php directory all files recursively

grep -Rl "@license" src/main/php | xargs sed -i "/@license/d"

Option Description:

-R or –dereference-recursive

For each directory operand, read and process all files in that directory, recursively, following all symbolic links.

-l or –files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of each file stops on the first match. (-l is specified by POSIX.)

sed, short for “stream editor”, allows you to filter and transform text.

sed -i Edit files in place

Few more related Articles

https://github.com/roopendra/devops/tree/master/bash_scripting

(Visited 1,805 times, 54 visits today)