Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read data from file

Status
Not open for further replies.

hammer1985

Programmer
Jan 17, 2006
2
BG
I am trying to write a perl script that reads a file and removes everything from the first "," to the end of the line.

Any ideas how can i do it?
 
Code:
open IN, "< file1";
open OUT, "> file2";

while (<IN>) {
   s/(,.*)$//;
   print OUT "$_\n";
}

try this

Corwin
 
index and substr might be quicker if you have a lot of entries and will be doing this on a regular basis.

Corwin's regex will work as well

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
BTW there's no need for the parentheses in that regexp, as you're not capturing anything you want to use later:
Code:
   s/,.*$//;
 
Alternatively, as a one-liner:
Code:
perl -pi -e 's/,.*$//' filename
 
and...

while(<>){
[tab]/,/;
[tab]print "$`\n";
}

Mike

I am not scrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top