LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-07-2009, 10:25 PM   #1
SQADude
LQ Newbie
 
Registered: Nov 2009
Posts: 7

Rep: Reputation: 0
Bash: Pattern matchin - how to do matching on a variable?


This is similar to the question asked on thread: http://www.linuxquestions.org/questi...-bash.-691571/

However, it's a bit different.

In perl, the code would be:
Code:
my $outPut = `ls -ltr foo`;
# The output of ls -ltr has 7 columns of data, with the filename
# in the last column


$outPut =~ /^*\ (\S+)$/;
my $fileName = $1;
The above code will parse the filename from the output of an ls -ltr command. How would I do that in BASH?
 
Old 12-07-2009, 10:52 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
awk would be well suited for this:
Code:
ls -ltr | awk '{ print $8 }'
But then what's the point of the -l flag?

Or, sometimes I do something like this with grep:

Code:
if [ `echo $foo | grep -Ec "regex"` -eq 1 ]; then
  # do stuff
else
  # do other stuff
fi

Last edited by jhwilliams; 12-07-2009 at 10:55 PM.
 
Old 12-07-2009, 10:54 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Same way described by ntubski in that thread - regex pattern matching. You can access the parenthesized matches in the BASH_REMATCH array.
 
Old 12-07-2009, 10:55 PM   #4
bit-head
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Rep: Reputation: 0
My point as well. You can use just:

Code:
ls -tr foo
to get the file name alone. Also you can make a single column with:

Code:
ls -1tr
(hint: that's a "one" not an "l"
 
Old 12-07-2009, 10:57 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
is getting file names so difficult? use shell expansion, and DON'T use ls -l + awk to get file names unless you are pretty sure files don't have spaces.
Code:
for myfile in *
do
 echo "$myfile"
done

Last edited by ghostdog74; 12-07-2009 at 10:58 PM.
 
Old 12-07-2009, 10:59 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Actually, you should *never* try to do any kind of parsing with the output of ls - http://mywiki.wooledge.org/ParsingLs
 
Old 12-07-2009, 11:00 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
8 columns--more if the file is a link

For the filename:
Code:
ls -l|sed -r 's/ +/ /g'|cut -d " " -f8
OR:
ls -l|awk '{print $8}'
 
Old 12-07-2009, 11:06 PM   #8
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
Quote:
Originally Posted by tuxdev View Post
Actually, you should *never* try to do any kind of parsing with the output of ls - http://mywiki.wooledge.org/ParsingLs
Well, the issue of having an \n embedded in a filename is not really a legitimate concern. Anyone who were to do this would surely be fired, and their files would be renamed by their replacement. But the unix/linux timestamp portability point is probably a legitimate cause for concern.
 
Old 12-08-2009, 12:20 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by tuxdev View Post
Actually, you should *never* try to do any kind of parsing with the output of ls - http://mywiki.wooledge.org/ParsingLs
not when trying to get other fields besides file names.
 
Old 12-08-2009, 12:41 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ghostdog74 View Post
not when trying to get other fields besides file names.
Even then, for maximum robustness; knowing the (possibly pathological!) filename, the "other fields" data can be found using the stat command.

Possibly over-defensive in many real-life situations and surely slow but appealing to those of us given aversion therapy in the school of "if it can go wrong, it will go wrong".
 
Old 12-08-2009, 01:23 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
well, ls uses the same "stat" libraries to retrieve those information as well. Also, stat may not be in some Unix platforms, but ls is.
 
Old 12-08-2009, 01:36 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ghostdog74 View Post
well, ls uses the same "stat" libraries to retrieve those information as well. Also, stat may not be in some Unix platforms, but ls is.
There are no right answers, only choices with various pros and cons. When deciding which choices to make, we balance the pros and cons, considering: maintainability, portability (thanks for that), skill level, robustness and time (both development and execution) ... .

The "sweet spot" will vary depending on usage -- a quick-and-dirty "one off", a cross-platform installation utility, a backup script, system-wide vs. private usage etc.

Are there many *n*x platforms that don't have stat -- apart from very cut-down versions such as BusyBox (which may have stat -- I don't know)?
 
Old 12-08-2009, 06:51 PM   #13
SQADude
LQ Newbie
 
Registered: Nov 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jhwilliams View Post
awk would be well suited for this:
Code:
ls -ltr | awk '{ print $8 }'
(I don't know awk or sed so bear with me - I might make incorrect comments)

If use the code above, it seems to work, as long as the filename does not have spaces in the filename .

Quote:
But then what's the point of the -l flag?
Without the -l flag, only the filenames are displayed. At least with the -ltr option, I know that the filename is anything that follows the time/date of the file.

Quote:
Or, sometimes I do something like this with grep:

Code:
if [ `echo $foo | grep -Ec "regex"` -eq 1 ]; then
  # do stuff
else
  # do other stuff
fi
I need to extract the filename, not do a comparison. I can do this easily in Perl. Not so easy in Bash.
 
Old 12-08-2009, 07:09 PM   #14
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
Quote:
Originally Posted by SQADude View Post
Without the -l flag, only the filenames are displayed. At least with the -ltr option, I know that the filename is anything that follows the time/date of the file.
This doesn't make sense to me at all.

Quote:
I need to extract the filename, not do a comparison. I can do this easily in Perl. Not so easy in Bash.
It isn't a comparison really -- that's a control structure you can use to perform various actions if a file name matches a regular expression. Anyway, use grep.
 
0 members found this post helpful.
Old 12-08-2009, 07:19 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,251

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
SQADude is saying that

ls -l

always produces one 'record' per file per line of output, and anything(!) after the date/timestamp in each rec is the filename, with or without spaces in it.
 
  


Reply

Tags
bash, matching, pattern, regex


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
Pattern matching in a bash case statement using regex ciphyre Programming 1 01-31-2009 12:20 PM
Pattern matching in BASH. ccin1492 Programming 8 12-19-2008 11:00 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM
pattern matchin with perl gene_gEnie Programming 6 09-29-2001 11:40 PM


All times are GMT -5. The time now is 02:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration