Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

search and replace

Posted on 1999-07-02
6
Medium Priority
?
138 Views
Last Modified: 2010-03-04
I have a text file containing:

test:12345:directory:name
foo:2345666:namedir:directory

I like to search for the line beginning with test
(first field) and replace the second field with *NP*
eg. the line above will become

test:*NP*:directory:name


Thanks
0
Comment
Question by:nngdemi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213730
This should work OK, provideing you want your outout in a new file, keeping your original file. Let me know if you want to change the file 'in place'.

open(IN_FH,"<in_name");
open(OUT_FH,">out_name");

while (<IN_FH> )
{
   if (/^test/)
   {
      @parts = split(/:/,$_);
      $parts[1] = 'NP';
      $fixed_line = join(/\*/,@parts);
      print(OUT_FH,$fixed_line);
   }
}
close(IN_FH);
close(OUT_FH);
0
 

Author Comment

by:nngdemi
ID: 1213731
This is almost what I want :-)

I just want the second field change to *NP*
and the rest stay the same:

for example:

file1.txt contains:
test:1234:dir:dir2
jim:3333333:dire:dir3
james:sf9sd9:dsfdsf:dir

after running the script, file1.txt should be:
test:*NP*:dir:dir2
jim:3333333:dire:dir3
james:sf9sd9:dsfdsf:dir

Thanks for your help

0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 200 total points
ID: 1213732
OK, to change inplace and keep your : delimiters

# test.pl
if (/^test/)
{
   @parts = split(/:/,$_);
   $parts[1] = '*NP*';
   $_ = join(/:/,@parts);
}

To run the program from the command line, use the -i option fo in place editing (saving original to file1.bak) and -p to wrap a loop around your script, reading a line at a time from the supplied file name argumants:

perl -pi.bak test.pl file1.txt  

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 1213733
perl -pi.bak -e "s/^test:[^:]*:/test:*NP*:/" file1.txt
0
 
LVL 2

Expert Comment

by:stam061398
ID: 1213734
teraplane :

What if you have a file like that :
test:12345:directory:name
foo:2345666:namedir:directory
test1:1232:dir:nm

Your solution will affect the first and the third line.

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213735
I am not sure what you mean. I thought you only wanted to change lines that start with 'test'. The foo.. line will still get written out, it just will not be altered.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question