LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 10: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, 10: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, 01:42 AM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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 01:48 AM. Reason: edit
 
Old 10-16-2013, 09:08 AM   #4
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 120

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, 05: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, 07:58 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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, 12:50 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 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
Open Source Consulting | Domain Registration