LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2017, 03:28 PM   #1
pradeepspa
Member
 
Registered: Oct 2015
Posts: 79

Rep: Reputation: Disabled
Find and replace string ina file with specific condition


All,

I am trying to do this

1. Find line of a text file by grepping specific string
2. Once found, replace one of the string in that line with other.

Am using below script,

Quote:
#!/bin/bash

while read line
do
grep $line /tmp/export/raw-file.txt |sed 's/torspgw2/stjnspgw1/g;s/calspgw1/torspgw2/g'`

done </tmp/export/grep-file.txt
But looks like this script output the value as expected but not actually editing the file with new value. Is there anything i need to add further to make this work?
 
Old 01-30-2017, 03:39 PM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,463
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
grep is not necessary or useful there. Read up more on plain sed which can do conditionals like this:

Code:
#!/bin/bash
while read line
do
        sed "/$line/{s/torspgw2/stjnspgw1/g;s/calspgw1/torspgw2/g}" /tmp/export/raw-file.txt
done </tmp/export/grep-file.txt

Last edited by Turbocapitalist; 01-30-2017 at 04:08 PM. Reason: /tmp/export/raw-file.txt
 
Old 01-30-2017, 03:59 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
Sorry to say, but neither solution presented will change the original file (I think Turbocapitalist might have forgotten the file name ).

Have a look at man sed for the -i option
 
Old 01-30-2017, 04:05 PM   #4
pradeepspa
Member
 
Registered: Oct 2015
Posts: 79

Original Poster
Rep: Reputation: Disabled
WOW!!! Awesome. Thanks guys.
 
Old 01-30-2017, 04:09 PM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,463
Blog Entries: 3

Rep: Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230Reputation: 2230
Quote:
Originally Posted by grail View Post
(I think Turbocapitalist might have forgotten the file name )
Thanks. Good catch.
 
  


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
find and replace a specific string linson_85 Linux - General 12 09-10-2015 11:44 AM
find and replace in each file if condition is true shridhar22 Linux - Newbie 7 02-14-2013 11:54 AM
How to find a file with specific string inside it. pinga123 Linux - Newbie 5 05-07-2010 07:30 AM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 08:10 AM
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 03:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration