LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-30-2011, 10:17 AM   #1
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Rep: Reputation: 0
Unhappy replace string in file using perl or sed


hi all

I have question about replacing a string in a file.

/etc/hosts

10.1.1.1 printera
10.1.1.1. printerb

how can I replace the printerb's 10.1.1.1 to something else(10.1.1.2, for example) without replace printera's 10.1.1.1 accidentally?

I have tried perl -e -pi "s/10.1.1.1/10.1.1.2/g" /etc/hosts. but, perl replace both 10.1.1.1 to 10.1.1.2.

any idea will be appreciated.

thanks in advance,

F.
 
Old 06-30-2011, 10:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
sed -i '/printera/s/10.1.1.1/10.1.1.2/' file
or
Code:
sed -i /printerb/!s/10.1.1.1/10.1.1.2/' file
 
Old 06-30-2011, 11:02 AM   #3
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
hi colucix

the sed works for this case. but I want to replace the specific string and apply the change into the file. that is why i used perl.

thanks,
 
Old 06-30-2011, 11:09 AM   #4
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
I found someone used this -

perl -pi -e 's/ReplaceMe/REPLACED/ if /MatchText/' filename

but it does not work for me.

idea?
 
Old 06-30-2011, 11:11 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You should take bit of time to read up on sed. It can do a whole lot more than just simple substitutions.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Read up on awk too, while you're at it, which is often the better tool to use when working with field-style input. It can be used here as well, although you have to move it through a temp file to save the changes.

Code:
awk '/printerb/ { $1="10.1.1.2" }1' /etc/hosts >tempfile
mv tempfile /etc/hosts
rm tempfile
A few useful awk references.
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.informatik.uni-hamburg.de.../gawk_toc.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
Old 06-30-2011, 11:29 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
the sed works for this case. but I want to replace the specific string and apply the change into the file. that is why i used perl
I am not sure you paid attention to the example because the -i option does make the change in the file.
 
Old 06-30-2011, 12:14 PM   #7
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
I am not sure you paid attention to the example because the -i option does make the change in the file.
my sed's version has no -i option at all. it is aix.
 
Old 06-30-2011, 12:23 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Well, don't you think that's something you should've mentioned at the beginning, then? You're probably not using bash either, right?

It's generally a good idea to spell out the full context of your situation, so we can provide you with useful answers, without wasting effort on things you can't use.

In any case, what's wrong with working through a temp file, as in my awk example above? It's only a couple of extra lines.


Oh yeah, and saying "it does not work for me", in your perl question above isn't very useful either. HOW did it fail? What exact command(s) did you use, and what output or error messages did you get?

Last edited by David the H.; 06-30-2011 at 12:26 PM. Reason: addendum
 
Old 06-30-2011, 12:26 PM   #9
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
sorry about that, David. we are using ksh in AIX environment.

let me think about your way.

Last edited by wf201626; 06-30-2011 at 12:27 PM.
 
Old 06-30-2011, 02:43 PM   #10
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
here is what i got -


10.1.1.1 printera
10.1.1.1. printerb

perl -piw -e 's/10.1.1.1/10.1.1.2/ if /\bprinta\b/' /etc/hosts. this will find the line contained exact matched key word and replace it the whatever you want to.

this perl one liner in my script will be generated by provided variables.

here is my soluton

#!/usr/bin/ksh

PRIP=10.1.1.1
OPRIP=10.1.1.2
PNAME printera

sub="s/${PRIP}/${OPRIP}/ if /\b${PNAME} \b/"
echo "perl -piw -e " "'${sub}'" " /etc/hosts" | ksh
 
Old 07-01-2011, 02:05 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Firstly, please use [code][/code] tags to help make your code readable.

May I ask the point of the following line:
Code:
echo "perl -piw -e " "'${sub}'" " /etc/hosts" | ksh
Why not just call your command?
Code:
perl -piw -e "${sub}" /etc/hosts
Also if you wish to try David's awk without a temp file you could do:
Code:
#!/usr/bin/ksh

PRIP=10.1.1.1
OPRIP=10.1.1.2
PNAME=printera

awk -voprip="$OPRIP" -vpname="$PNAME" '$NF == pname{$1 = oprip}{print > FILENAME}' /etc/hosts
 
Old 07-01-2011, 02:22 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An alternative is the ancient ed editor:
Code:
#!/bin/ksh
ed -s testfile <<-EOF
   /printerb/s/10.1.1.1/10.1.1.2/
   w
   q
EOF
 
Old 07-04-2011, 08:59 AM   #13
wf201626
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Firstly, please use [code][/code] tags to help make your code readable.

May I ask the point of the following line:
Code:
echo "perl -piw -e " "'${sub}'" " /etc/hosts" | ksh
Why not just call your command?
Code:
perl -piw -e "${sub}" /etc/hosts
Also if you wish to try David's awk without a temp file you could do:
Code:
#!/usr/bin/ksh

PRIP=10.1.1.1
OPRIP=10.1.1.2
PNAME=printera

awk -voprip="$OPRIP" -vpname="$PNAME" '$NF == pname{$1 = oprip}{print > FILENAME}' /etc/hosts
hi Grail

this one liner is ok to run in command-line directly. if you run it in a script,
Code:
perl -piw -e "${sub}" /etc/hosts
will not run correctly.

I will give awk a try and let you know how it works in my env.

thank, Grail
 
Old 07-05-2011, 02:29 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
will not run correctly.
Well I do not have access to ksh at the moment, but it should not matter as you essentially are not using
anything ksh-centric, and it works just fine inside the script (as I would expect seeing the script is just a wrapper).

When you say it doesn't run correctly, what do you mean?

Here is what I tested on:

infile:
Code:
10.1.1.1 printera
10.1.1.1 printerb
script.sh:
Code:
#!/bin/bash

PRIP=10.1.1.1
OPRIP=10.1.1.2
PNAME printera

sub="s/${PRIP}/${OPRIP}/ if /\b${PNAME}\b/"

perl -piw -e "${sub}" infile
And when run:
Code:
$ ./script.sh
$ cat infile
10.1.1.2 printera
10.1.1.1 printerb
 
  


Reply



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
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 26 09-26-2021 04:10 PM
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-25-2007 11:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 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
Open Source Consulting | Domain Registration