LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2008, 08:40 AM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
perl question


hi guys;

could anyone tell how to remove anything after comma in each line using perl (Assuming a large file in Unix)

Text lineslooks like:

7345_v,8042404....
7346_v,8042405....
7347_v,8042406....
7348_v,8042407....
.
.
.
.
etc

thanks much
 
Old 11-26-2008, 09:17 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
You could do this from the command-line with Perl as well, but here's a small script:
Code:
#!/usr/bin/perl
use strict;
use warnings;

#$^I = '.bak';

while (<>) {
  s/,.*//;
  print;
}
That replaces [the comma and everything after it] with nothing. (If you want to keep the comma, use s/,.*/,/ instead of what I have above.) Save this as, say, edit and then run it as perl edit file_name. If the output looks sane, then uncomment the line #$^I = '.bak' and run it again. Once you uncomment that line, the file will actually be changed and, for no charge, Perl will make a backup as file_name.bak. That way if you did something very wrong, you have a backup. Edit - now that I think of it, can I get credit for your homework?

Last edited by Telemachos; 11-26-2008 at 09:26 AM.
 
Old 11-26-2008, 09:31 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
This was runnnig awesome - thank you so much ...

Quote:
Originally Posted by Telemachos View Post
You could do this from the command-line with Perl as well, but here's a small script:
Code:
#!/usr/bin/perl
use strict;
use warnings;

#$^I = '.bak';

while (<>) {
  s/,.*//;
  print;
}
That replaces [the comma and everything after it] with nothing. (If you want to keep the comma, use s/,.*/,/ instead of what I have above.) Save this as, say, edit and then run it as perl edit file_name. If the output looks sane, then uncomment the line #$^I = '.bak' and run it again. Once you uncomment that line, the file will actually be changed and, for no charge, Perl will make a backup as file_name.bak. That way if you did something very wrong, you have a backup. Edit - now that I think of it, can I get credit for your homework?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl question dnoy Programming 9 01-04-2008 03:06 PM
Perl Question for perl hackers. linuxlover1 Programming 1 06-27-2006 06:56 PM
reinstall perl question.. (urpme perl) rjcrews Mandriva 2 01-28-2006 06:19 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration