LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-04-2009, 03:26 PM   #1
swaroop.tata
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Rep: Reputation: 0
search for a pattern and comment the line


Hello,

I am trying to write some perl code to search for a pattern in a file, if i found the pattern, i want to comment (put a #before the line) it out.

Is it possible through perl.

Something like this.


#!/usr/bin/perl -w

open(FILE,"test.txt") or die "Unable to open file\n";

while(<FILE>) {
my $line =$_;
if ($line =~ /Hello/) {
print "Match found: $line\n";
s/$line/#$line/;
}
}
 
Old 09-04-2009, 04:35 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
i think you have the right idea. whenever im manipulating a file like this, i read one in (read only) and create a new one (write only), ive never used the same file for read/write (replacing) like you want to do, but it can be done either way of course. the only real thing your missing is writing the line to the file. a simple print statement is all you need. however, i would write it using the following pseudocode:

Code:
- open "IN" file for read
- open "OUT" file for write (create it)
- while not EOF:
-- read "IN" file and save to "$line"
-- if "$line" matches the pattern:
---- set "$line" to: "#" + "$line"
-- print "$line" to "OUT" file
- close files
in either case (whether the line matched the pattern or not), the line is still written to the output/new/modified file. does this help?

Last edited by nadroj; 09-04-2009 at 04:46 PM.
 
Old 09-04-2009, 07:23 PM   #3
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
You could do it as a one liner as well, something like

perl -pi -e '$_ = "#".$_ if m/Hello/;' input_file.txt

it does the edit in place by renaming the input file and opening an new output file with the original name


[edit, cleaner one liner]
perl -pi -e 'print "#" if m/Hello/;' input_file.txt

Last edited by estabroo; 09-04-2009 at 07:26 PM. Reason: simplification
 
  


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
search for inverse of pattern and join with the line before it akelder Programming 18 07-14-2009 07:46 PM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Putting blank line after the search pattern. dina3e Programming 2 09-21-2008 07:38 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

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

All times are GMT -5. The time now is 04:20 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