LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-12-2011, 05:02 PM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
[bash] globbing - specific filenames


Code:
cat ${PREFIX}[0-9][0-9][0-9][0-9] ${PREFIX}[0-9][0-9][0-9][0-9][0-9][0-9] >list
I am specifically looking for file names of ending in 4 or 6 digits. Can this be expressed in a smarter way?
 
Old 05-12-2011, 05:19 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
This should work:
Code:
$ prefix="abcd"
$ ls | sed -nr "/$prefix([0-9]{4}$|[0-9]{6}$)/p"
abcd4444
abcd666666
 
1 members found this post helpful.
Old 05-12-2011, 05:30 PM   #3
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Brilliant! Just need one small correction or we're also catching files where $prefix is preceded by other characters:

Code:
ls | sed -nr "/^$prefix([0-9]{4}$|[0-9]{6}$)/p"
 
Old 05-12-2011, 05:33 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
I'm glad that's what you wanted. Please don't forget to mark the thread solved.
 
Old 05-12-2011, 06:09 PM   #5
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Oops - just noticed it's not happy about $PREFIX containing a relative pathname:

PREFIX=dir/fileprefix
 
Old 05-12-2011, 06:23 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Either change the prefix to escape the forward slash:
Code:
PREFIX=dir\/fileprefix
or change the delimiter to something else, eg:

Code:
ls | sed -nr ":^$prefix([0-9]{4}$|[0-9]{6}$):p"
 
1 members found this post helpful.
Old 05-13-2011, 03:23 AM   #7
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
Now this is ingenious! We just need to tell sed to change the delimiter to get the above to work:

When using sed for substitution, the character following the 's' is automatically taken as the delimiter. For patterns the non-default delimiter needs to be introduced with a backslash:

Code:
ls | sed -nr "\:^$prefix([0-9]{4}$|[0-9]{6}$):p"

Thank you for your time, sycamorex.

Last edited by hashbang#!; 05-13-2011 at 03:26 AM.
 
1 members found this post helpful.
Old 05-13-2011, 03:36 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
For patterns the non-default delimiter needs to be introduced with a backslash:
Indeed. I forgot about it.
 
Old 05-13-2011, 05:00 AM   #9
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
I spent 10 minutes reasearching sed options; I learnt more from that than I would have done had it worked out of the box!
 
  


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
[Bash] Rename filenames with corresponding filenames Power2All Linux - Software 4 12-02-2009 04:15 AM
Karmic - bash globbing gone .... ??? syg00 Ubuntu 1 11-05-2009 06:25 AM
Learning Bash, potential bug or total misunderstanding regarding globbing. crispyleif Linux - Newbie 16 02-07-2009 02:32 PM
LXer: Bash Extended Globbing LXer Syndicated Linux News 0 10-09-2008 11:10 AM
Globbing Problem in BASH Hink Programming 2 07-28-2005 12:19 PM

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

All times are GMT -5. The time now is 04:01 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