LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-09-2010, 01:14 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Perl command that's similar to cut in bash?


I have a perl script that grabs a file but I need to grab a chunk of that file name and store it in a variable. I would like to move away from my current ugly implementation of using bash/perl when I'm sure perl is capable of doing so.

for example, I want to take the given file:
abc_123.zip

and store just 123 in a variable. How can I do this in perl? Without modules would be preferable. Thanks!
 
Old 07-09-2010, 01:55 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
substr

Code:
$text = "abc_123.zip";

$newtext = substr ($text, 4, 3);

print $newtext . "\n";

Last edited by smoker; 07-09-2010 at 01:57 PM.
 
Old 07-09-2010, 02:16 PM   #3
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
Thanks!
 
Old 07-09-2010, 07:45 PM   #4
katharnakh
LQ Newbie
 
Registered: Aug 2006
Posts: 5

Rep: Reputation: 0
I would have used regex, instead of substr function. Because with regex one is able to grab whatever he likes without index.
for eg:
Code:
$text = "abc_123.zip";
$text =~ /(\d+)\.\w+$/#grab numbers that occurs before file-extension.
print $text;
 
1 members found this post helpful.
Old 07-09-2010, 08:33 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
the equivalent of "cut" in Perl is the split() function.
Code:
split /PATTERN/,EXPR
in /PATTERN/, specify the "_" and "." characters. Then the string you need (eg abc_123.zip) will be the 2nd field
 
Old 07-09-2010, 09:30 PM   #6
timetraveler
Member
 
Registered: Apr 2010
Posts: 243
Blog Entries: 2

Rep: Reputation: 31
$f = 'file_123.zip';

($fnum) = $f =~ m/_([^\.]+)\./;
 
Old 07-09-2010, 09:33 PM   #7
timetraveler
Member
 
Registered: Apr 2010
Posts: 243
Blog Entries: 2

Rep: Reputation: 31
Quote:
Originally Posted by katharnakh View Post
I would have used regex, instead of substr function. Because with regex one is able to grab whatever he likes without index.
for eg:
Code:
$text = "abc_123.zip";
$text =~ /(\d+)\.\w+$/#grab numbers that occurs before file-extension.
print $text;


That's wrong.

Instead of print $text it should be
print $1
 
Old 07-10-2010, 03:10 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
the equivalent of "cut" in Perl is the split() function.
Code:
split /PATTERN/,EXPR
in /PATTERN/, specify the "_" and "." characters. Then the string you need (eg abc_123.zip) will be the 2nd field
Well, yes, but then for completeness:
Code:
my $desired_field = (split(/$pattern/, $line))[$desired_field_number_stating_from_0];
.
 
Old 07-10-2010, 03:37 AM   #9
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
All these solutions are good in their own place.
substr is good if you can guarantee the length of the substring you want.
regex is good if you can guarantee the format of the substring you want.
split is good if you can guarantee the patterns to separate the fields by.

Horses for courses.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash cut command to get df percentage nutthick Programming 8 12-16-2010 12:02 PM
help with cut command using find. Cut last 8 characters leaving the rest ncsuapex Programming 4 09-16-2009 09:55 PM
a little bash help with the 'for' command (or similar) checkmate3001 Linux - General 5 02-18-2009 01:52 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
Does anything similar to Apple's Final Cut Pro exist for Linux? lorengd Linux - Software 11 02-09-2004 01:36 PM

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

All times are GMT -5. The time now is 07:32 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration