Solved

read a file and replace certain lines

Posted on 2002-06-13
5
113 Views
Last Modified: 2010-08-05
All,

How would I read through a file and replace lines that contain a certain word?

e.g.

The following test file is in existence

name,  age,  interests
bill,  30 ,  Football
Mark,  15 ,  Computers
Jill,  10 ,  Nothing!

If I wanted to loop through this file and replace the line containing Mark's age and interests with something else how would I go about that?

0
Comment
Question by:cyrobinson
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7075402
perl -i.bak -pe 's/Mark/whatever/' file
0
 
LVL 10

Expert Comment

by:rj2
ID: 7076439
If you want to change Marks age to 35 and his interests to Women, you could try sample below.
Just paste the line into a DOS prompt. Change filename from data.txt to correct filename in your system.

perl -i.bak -pe "s/Mark,(\s*)\d*(\s*),(\s*)\w*/Mark,\1\Q35\E\2,\3Women/" data.txt

If you don't need to preserve the spaces you could try the somewhat simpler line below instead.

perl -i.bak -pe "s/Mark,.*/Mark,35,Women/" data.txt

0
 

Author Comment

by:cyrobinson
ID: 7079974
i was more wanting to loop over each line of an input file from within a perl script and modify each line like that
0
 
LVL 10

Accepted Solution

by:
rj2 earned 200 total points
ID: 7080182
The one-liner above does that.
But here it is like a regular script (not one-liner)

#!/usr/bin/perl
use strict;

my($file)="data.txt";
my($backupfile)="data.bak";

unlink($backupfile);
rename($file,$backupfile);
open(INFILE,$backupfile);
open(OUTFILE,">$file");
while(<INFILE>) {          
     s/Mark,(\s*)\d*(\s*),(\s*)\w*/Mark,\1\Q35\E\2,\3Women/;    
     print OUTFILE $_;
}
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7080190
>  was more wanting to loop over each line of an input file from within a perl script and modify each line like that

This is exactly what my suggestion does :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now