![]() |
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. |
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 |
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 |
..and even better, you don't even need the "cat".
|
Quote:
sed -i 'stuff' filename sed 'stuff' filename > newfilename etc. You almost never need cat--or dog for that matter...;) |
Quote:
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? |
It's because of the grep - use pixellany solution.
|
Quote:
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, |
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. |
Code:
awk '/_Id/ {$2=100} |
Quote:
|
| All times are GMT -5. The time now is 07:07 AM. |