LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to identify a line and replace another string on that line using Shell script? (http://www.linuxquestions.org/questions/programming-9/how-to-identify-a-line-and-replace-another-string-on-that-line-using-shell-script-588711/)

Sid2007 10-01-2007 04:15 PM

How to identify a line and replace another string on that line using Shell script?
 
Folks,
I have a requirement similar to 'search and replace' but a little different. Basically I need to write a shell script that looks for a pattern/keyword to identify lines to be modified in a file, and then search/replaces another string on that line.

Say for example, I have the following definition in a file:

Emp_Name String;
Department_Id String;
Emp_number String;
Emp_Salary Number;

What I am looking for is this:
1. Search for lines with '_id' occurance, then change the 'String' to a 'Number'.
2. Generate this into a new file.


Any pointers will be deeply appreciated. Thanks.

osor 10-01-2007 04:34 PM

Iím not exactly sure what the problem is. IIUC, would this work?
Code:

$ grep -- '_Id' original_file | sed -e 's/String/Number/' > new_file

pixellany 10-01-2007 04:40 PM

This is a job for SED....

Here is an example:
cat filename | sed '/th/s/a/b/g' > newfilename

This searches filename and--for every line containing "th"--replaces all occurences of "a" with "b". It then writes the data to a new file: "newfilename".

the more general syntax is <address>, followed by the "s" command (substitute), with a "g" to make it global for the line---ie to change all instances.

My favorite SED tutorial is here: http://www.grymoire.com/Unix/Sed.html

syg00 10-01-2007 04:57 PM

..and even better, you don't even need the "cat".

pixellany 10-01-2007 05:22 PM

Quote:

Originally Posted by syg00 (Post 2909747)
..and even better, you don't even need the "cat".

Indeed...

sed -i 'stuff' filename
sed 'stuff' filename > newfilename
etc.

You almost never need cat--or dog for that matter...;)

Sid2007 10-01-2007 05:54 PM

Quote:

Originally Posted by osor (Post 2909727)
Iím not exactly sure what the problem is. IIUC, would this work?
Code:

$ grep -- '_Id' original_file | sed -e 's/String/Number/' > new_file

Hi, Thanks for the pointers. However, I noticed this command only writes the filtered lines ( _id lines ) to the new file.

What I am looking for is to simply replace the oldfile data, based on the filter/search/replace and get a new file.

Any pointers?

syg00 10-01-2007 06:15 PM

It's because of the grep - use pixellany solution.

Sid2007 10-01-2007 06:29 PM

Quote:

Originally Posted by pixellany (Post 2909768)
Indeed...

sed -i 'stuff' filename
sed 'stuff' filename > newfilename
etc.

You almost never need cat--or dog for that matter...;)

Hi this works! Thank you so much.

One more thing. If I need to add additional key words to scan and replace the same filw how can I do that?

That is for example, in addition to _id, I also need to look for lines with _name column to do the search/replace.

Thanks,

syg00 10-01-2007 06:45 PM

I would suggest some time spent reading the tutorial mentioned above might be worthwhile.
There are (at least) a couple of ways of doing what you want in a single pass.

ghostdog74 10-01-2007 08:30 PM

Code:

awk '/_Id/ {$2=100}
    /_number/{$2=102}
    {print}' "file"


osor 10-01-2007 08:49 PM

Quote:

Originally Posted by Sid2007 (Post 2909790)
HHowever, I noticed this command only writes the filtered lines ( _id lines ) to the new file.

Sorry about that. From the original post, it seemed like thatís what you wanted. Carry on.


All times are GMT -5. The time now is 04:04 PM.