LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2010, 11:05 AM   #1
andiramesh_m
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
Question Perl command to replace string by line numbe


Hi,
I want to know the Perl command to replace a string by pointing the line number. I know how to replace a string without pointing a line number but I am in need to replace only the two matching string in a file. Any help is appreciated.

Thanks
 
Old 07-11-2010, 04:49 AM   #2
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
I'm not sure what "pointing to" means here, but if I'm reading correctly, you want to replace a text string with the current line number?

I don't know about perl, but it's simple to do in (gnu) awk.
Code:
awk 'gsub(/string/,NR)1' filename
 
Old 07-11-2010, 05:02 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Maybe it's just me, but that as a first post, seems like homework.
 
Old 07-13-2010, 11:06 PM   #4
andiramesh_m
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
David,
Thanks for your help.

I am looking for a equivalent perl command like the below sed command.

eg:
sed '100s/error/debug/g' <file name>

(This will replace the word "error" to "debug" at line number 100).

Similarly what is the perl command to replace a string available at x line number?

Sed command would not modify the existing file rather we should redirect it to a new file but perl would directly modify the existing file
 
Old 07-13-2010, 11:08 PM   #5
andiramesh_m
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
David,
Thanks for your help.

I am looking for a equivalent perl command like the below sed command.

eg:
sed '100s/error/debug/g' <file name>

(This will replace the word "error" to "debug" at line number 100).

Similarly what is the perl command to replace a string available at x line number?

Sed command would not modify the existing file rather we should redirect it to a new file but perl would directly modify the existing file
 
Old 07-14-2010, 04:12 AM   #6
vigilandy
Member
 
Registered: Mar 2010
Location: Tokyo
Distribution: Arch, Fedora
Posts: 71

Rep: Reputation: 23
perhaps you need to look at the -i option for sed.
 
Old 07-15-2010, 12:00 PM   #7
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
I know hardly anything about perl syntax, but after a quick google search and a couple of minutes on this site, I got this to work:
Code:
perl -i -ple 's/error/debug/g if $. == 100; close ARGV if eof' file
Apparently the last part is only necessary if you're processing multiple files, as you need to reset the $. count for each new file.

There are also examples there on using ranges and how to exclude lines. Check it out.
 
1 members found this post helpful.
  


Reply

Tags
command, line, number, perl, replace, string



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
help...how to replace string using Perl adrian99420 Programming 2 05-05-2008 07:34 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
Perl : Troubles to replace a string. philipina Programming 4 07-26-2004 05:09 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:11 AM.

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