Jordan Wells Jordan Wells - 1 year ago 135
Bash Question

Script to sed lines containing specific words

I am looking at writing quite a basic script that firstly just cats through a textfile I have, and for every line that contains the world 'NUMERIC' just runs a sed command. I was wondering how I could potentially go about doing this?

So basically I firstly have a very large text file, this script needs to run some sort of for loop (or something else) to go through and run a sed command only if the line in the text file contains the word 'NUMERIC'


Answer Source

or why not do this:

cat file.txt | grep 'NUMERIC' | sed 's/abc/def/'

In above example, i am catting the file "file.txt", and then grepping the string 'NUMERIC', which will give me lines which has 'NUMERIC' in them, and then applying a sed operation replacing abc to def (replace the SED operation of your choice).

The above command will print the output on screen. If you want to save it, you can redirect the output to a file.

example below:

cat file.txt | grep 'NUMERIC' | sed 's/abc/def/' >> new_file.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download