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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete to end of line 1

Status
Not open for further replies.

pflakes

Technical User
Jan 27, 2004
31
US
I'm working on a script in which I'm reading in a file, then searching for the first comma in each line - that I can do. What I want to do next is delete everything beginning with the comma to the end of the line.

Is there a simple way to do this?
 
Supposing $line is your line of text, and $comma is the position of the first comma that you have already found:

Code:
$line = substr($line, 0, $comma - 1)

... should do the trick. It will return everything from the beginning of the line up to the character before the position of the comma.
 
If that's all that you want your script to do, it can be easily achieved in a one-liner - UNTESTED - BACK UP YOUR FILE BEFORE YOU TRY THIS:
Code:
perl -pi -e 's/,.+$//' filename.txt
 
That took care of the problem. Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top