LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-01-2007, 04:15 PM   #1
Sid2007
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Rep: Reputation: 0
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.
 
Old 10-01-2007, 04:34 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
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
 
Old 10-01-2007, 04:40 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
Old 10-01-2007, 04:57 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,094

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
..and even better, you don't even need the "cat".
 
Old 10-01-2007, 05:22 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by syg00 View Post
..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...
 
Old 10-01-2007, 05:54 PM   #6
Sid2007
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by osor View Post
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?
 
Old 10-01-2007, 06:15 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,094

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
It's because of the grep - use pixellany solution.
 
Old 10-01-2007, 06:29 PM   #8
Sid2007
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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,
 
Old 10-01-2007, 06:45 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,094

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
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.
 
Old 10-01-2007, 08:30 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
awk '/_Id/ {$2=100}
     /_number/{$2=102}
    {print}' "file"
 
Old 10-01-2007, 08:49 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by Sid2007 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration