Skip to content
Advertisement

for each line of a file, grep a specific string and make string substitution

I have a file containing more than 14000 records. What I want to do is to process this file line by line and replace a String by anodher string returned by grep command. For example: Line :

/xxxxx/xxxxx/Class.java:67: Logger.w(TAG, "message");

My grep command to get Class.java string is (Class.java is juste an example):

grep -o '[a-zA-Z]*"*.java"*'

I must, for each line, replace the TAG string by the class.java string return by grep command

Advertisement

Answer

You can use sed and do the following:

sed -r 's#(.*)/(.*).java(.*)(TAG)#1/232#g'

Characters surrounded with parenthesis are groups that you can use in the second part to get their content.

In order to modify the file in-place, you should:

sed -ir 's#(.*)/(.*).java(.*)(TAG)#1/232#g' your_file.txt
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement