read a file and replace certain lines

Posted on 2002-06-13
Last Modified: 2010-08-05

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


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?

Question by:cyrobinson
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
  • 2
  • 2
LVL 51

Expert Comment

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

Expert Comment

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


Author Comment

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
LVL 10

Accepted Solution

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

use strict;


while(<INFILE>) {          
     print OUTFILE $_;
LVL 51

Expert Comment

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 :-)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
transpose into pipe delemited 8 81
perl: Cleaning meta tags using RegEX 12 88
Perl File::Find alternative 1 87
Export Variables in Perl 3 108
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

696 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