LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-14-2013, 11:25 PM   #1
lindextop
LQ Newbie
 
Registered: Aug 2013
Posts: 6

Rep: Reputation: Disabled
Problem with Pattern Matching


Hi,
I am not understanding the working of the following pattern matching commands :

I want to list files in the current directory that matches a specific pattern.

Here is what i have

(1)ls 0000000[1-5].jpg

00000001.jpg 00000002.jpg 00000003.jpg 00000004.jpg 00000005.jpg

Then why are the following commands not working?

(2)ls 00000[001-005].jpg
ls: cannot access 00000[001-005].jpg: No such file or directory

(3)ls [00000001-00000005].jpg
ls: cannot access [00000001-00000005].jpg: No such file or directory

(4)ls [00000001-00000005]*.jpg
(This lists all files in directory without regard to the pattern)

Can anybody help me out in understanding the problem in the above commands.I am using /bin/bash

thanks in advance
lin
 
Old 10-14-2013, 11:58 PM   #2
lindextop
LQ Newbie
 
Registered: Aug 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
hi,
i think i understand the problem while framing the question!!!

within [],it matches any single charachter only.
so if i want to get files between 00000001.jpg and 00000005.jpg
i should give 0000000[1-5].jpg only.

I got it confused between {..} expansion,I think.

I am not sure how the below case works though.

(4)ls [00000001-00000005]*.jpg
(This lists all files in directory without regard to the pattern)


Now,how do i get a pattern for listing all files between(and including) 00000001.jpg and 00000716.jpg
I tried

ls 00000[0-7]?[0-6].jpg

But this wont list files like 00000019.jpg, 00000708.jpg etc.

thanks in advance.
lin
 
Old 10-16-2013, 02:42 AM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,102

Rep: Reputation: 181Reputation: 181
try this, create a bash script.

open your favorite editor, use nano, vi or whichever editor you prefer.

copy and paste the code below to your favorite text editor.

after pasting the code save it with a ".sh" file name extension such as "lxx.sh"

after saving the code and exiting from the editor.

type this at your terminal: chmod +x lxx.sh



create myoutput.txt file or whatever file name you want.

if you want to use other file name, please change the variable on the script also.


to run the script, make sure it's already executable and the output file you already have created or else there will be an error.

here's the code snippet, hope it helps

Quote:
#!/bin/bash
x=1
while [ $x -le 99999999 ]
do

x=$(( $x + 1 ))

if [ $x -le 9 ]

then
y="00000000"
z=".jpg"
ls "$y$x$z" >> ./myoutput.txt

elif [ $x -le 99 ]

then
y="000000"
ls "$y$x$z" >> ./myoutput.txt

elif [ $x -le 9999 ]

then
y="0000"
z=".jpg"
ls "$y$x$z" >> ./myoutput.txt

elif [ $x -le 99999 ]

then
y="000"
z=".jpg"
ls "$y$x$z" >> ./my output.txt

elif [ $x -le 999999 ]

then
y="00"
z=".jpg"
ls "$y$x$z" >> ./my output.txt

elif [ $x -le 9999999 ]

then
y="0"
z=".jpg"
ls "$y$x$z" >> ./my output.txt

else [ $x -gt 9999999 ]

then
z=".jpg"
ls "$x$z" >> ./my output.txt

fi

done

Last edited by JJJCR; 10-16-2013 at 02:48 AM. Reason: edit
 
Old 10-16-2013, 10:08 AM   #4
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 118

Rep: Reputation: Disabled
Quote:
Originally Posted by lindextop View Post
hi,
i think i understand the problem while framing the question!!!

within [],it matches any single charachter only.
so if i want to get files between 00000001.jpg and 00000005.jpg
i should give 0000000[1-5].jpg only.

I got it confused between {..} expansion,I think.

I am not sure how the below case works though.

(4)ls [00000001-00000005]*.jpg
(This lists all files in directory without regard to the pattern)


Now,how do i get a pattern for listing all files between(and including) 00000001.jpg and 00000716.jpg
I tried

ls 00000[0-7]?[0-6].jpg

But this wont list files like 00000019.jpg, 00000708.jpg etc.

thanks in advance.
lin
You are on the right track, bracket expansion does match one character at a time. So to get all files 00000001.jpg and 00000716.jpg inclusive, you can do:

Code:
$ ls 00000[0-9][0-9][0-9].jpg
http://www.tldp.org/LDP/Bash-Beginne...ect_04_02.html
 
Old 10-20-2013, 06:41 AM   #5
lindextop
LQ Newbie
 
Registered: Aug 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thankyou JJJCR and GNU/Linux for your help.

Suppose the directory contains files starting from say 00000001.jpg to 00001000.jpg
I want to select files between/including 00000001.jpg and 00000716.jpg
The command `ls 00000[0-7][0-9][0-6]` also lists files like 00000756.jpg which is unwanted.

I solved it later using ls {00000001..00000716}.jpg
 
Old 10-20-2013, 08:58 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,102

Rep: Reputation: 181Reputation: 181
Question

Quote:
Originally Posted by lindextop View Post
Thankyou JJJCR and GNU/Linux for your help.

Suppose the directory contains files starting from say 00000001.jpg to 00001000.jpg
I want to select files between/including 00000001.jpg and 00000716.jpg
The command `ls 00000[0-7][0-9][0-6]` also lists files like 00000756.jpg which is unwanted.

I solved it later using ls {00000001..00000716}.jpg
{start_range..end_range} that's why it works
 
Old 10-20-2013, 01:50 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
That works does it??? I am wondering how it will go matching 00000009.jpg? I believe this is between your criteria numbers but will never be matched.
 
  


Reply

Tags
bash, pattern, regular expressions, script, shell


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
Matching patterns or partial pattern matching yaplej Programming 6 12-16-2012 11:21 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 01:17 PM
Pattern Matching Problem ratul_11 Programming 3 12-28-2007 01:27 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 12:05 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 05:40 AM


All times are GMT -5. The time now is 08:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration