LinuxQuestions.org
Review your favorite Linux distribution.
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 04-23-2009, 09:51 AM   #1
kais1
Member
 
Registered: Dec 2008
Posts: 61

Rep: Reputation: 17
grep for a particular entry in the file name


Dear all,


I have a folder with following list of files with names like

amg-9876544-M37421-3000282.PDF
amg-9876544-M37408-3000457.PDF
amg-9876544-M37488-3000260.PDF
amg-9876544-M37481-3300063.PDF
amg-9876544-M37454-3000675.PDF
amg-9876544-M37543-3000672.PDF
amg-9876544-M37541-3363000.PDF
amg-9876544-M37528-3000381.PDF
amg-9876544-M37524-3000432.PDF
amg-9876544-M37506-3000352.PDF
amg-9876544-M37500-3021100.PDF
amg-9876544-M37585-3850050.PDF
amg-9876544-M37577-3005105.PDF
amg-9876544-M37565-3000502.PDF
amg-9876544-M37611-3300527.PDF
amg-9876544-M37610-3300150.PDF
amg-9876544-M37607-3300259.PDF


I want to list all the files that has the entry with 33..for ex..file with name amg-9876544-M37607-3300259.PDF after the -.

If I give ls -rlt | grep *33* it lists all the file with entry 33 anywhere in it.I want to grep for files with 33 after that "-" in the file name..

Please suggest

Kai
 
Old 04-23-2009, 10:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
If the file names have the same number of characters and digits, as in your example, you can simply use the ? wildcard to match any single character:
Code:
ls -lrt amg-???????-??????-33?????.PDF
 
Old 04-23-2009, 10:11 AM   #3
rabbit2345
Member
 
Registered: Apr 2007
Location: 中国上海
Distribution: openSUSE 11.3
Posts: 371

Rep: Reputation: 40
Put a period in front of the '-'. So it looks like:

Code:
ls -rlt | grep .-33
 
Old 04-23-2009, 10:13 AM   #4
rabbit2345
Member
 
Registered: Apr 2007
Location: 中国上海
Distribution: openSUSE 11.3
Posts: 371

Rep: Reputation: 40
Put a . in front of the dash.

ls -rlt | grep .-33
 
Old 04-23-2009, 10:19 AM   #5
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by colucix View Post
If the file names have the same number of characters and digits, as in your example, you can simply use the ? wildcard to match any single character:
Code:
ls -lrt amg-???????-??????-33?????.PDF
Thanks Colucix,but it doesn't work

ls -lrt | grep amg-????????-????????-33??????.PDF

doesn't display anything..


am sure that I replaced all the places with q marks


Please advise


Kai
 
Old 04-23-2009, 10:21 AM   #6
Libu
Member
 
Registered: Oct 2003
Location: Chennai
Distribution: Slackware 12.1
Posts: 165

Rep: Reputation: 36
You don't have to use grep for this
Quote:
ls *-33*
 
Old 04-23-2009, 10:21 AM   #7
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by rabbit2345 View Post
Put a . in front of the dash.

ls -rlt | grep .-33


Thanks Rabbit2345,


but it doesn't help

ls -lr | grep *.-33* (or) ls -lr | grep .-33

doesn't display anything.


Please advise

Kai
 
Old 04-23-2009, 10:29 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by kais1 View Post
Thanks Colucix,but it doesn't work

ls -lrt | grep amg-????????-????????-33??????.PDF

doesn't display anything..
This is really strange. Are you sure you are in the right directory? It must list the files you've shown in the post above.
Code:
$ ls -l
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37408-3000457.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37421-3000282.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37454-3000675.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37481-3300063.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37488-3000260.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37500-3021100.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37506-3000352.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37524-3000432.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37528-3000381.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37541-3363000.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37543-3000672.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37565-3000502.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37577-3005105.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37585-3850050.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37607-3300259.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37610-3300150.PDF
-rw-r--r-- 1 colucix users   0 Apr 23 16:24 amg-9876544-M37611-3300527.PDF
$ ls -lrt amg-???????-??????-33?????.PDF
-rw-r--r-- 1 colucix users 0 Apr 23 16:24 amg-9876544-M37611-3300527.PDF
-rw-r--r-- 1 colucix users 0 Apr 23 16:24 amg-9876544-M37610-3300150.PDF
-rw-r--r-- 1 colucix users 0 Apr 23 16:24 amg-9876544-M37607-3300259.PDF
-rw-r--r-- 1 colucix users 0 Apr 23 16:24 amg-9876544-M37541-3363000.PDF
-rw-r--r-- 1 colucix users 0 Apr 23 16:24 amg-9876544-M37481-3300063.PDF
$
 
Old 04-23-2009, 01:31 PM   #9
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Thanks a lot Colucix, it works fine..

Can you also tell me how can I move this conditioned files to another folder in the same file system


Thanks
Kai
 
Old 04-23-2009, 03:43 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by kais1 View Post
Can you also tell me how can I move this conditioned files to another folder in the same file system
Just use the mv command:
Code:
$ mv amg-???????-??????-33?????.PDF /path/to/destination/dir
the shell will expand the wildcards with the matching file names and move all of them to the destination folder. The command actually executed will be:
Code:
mv amg-9876544-M37481-3300063.PDF amg-9876544-M37541-3363000.PDF amg-9876544-M37607-3300259.PDF amg-9876544-M37610-3300150.PDF amg-9876544-M37611-3300527.PDF /path/to/destination/dir
 
  


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
grep for string in file after a certain part of the file B-Boy Programming 6 02-18-2009 08:49 AM
known_hosts file entry RaduH Linux - General 1 11-28-2007 02:04 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
Log File entry Mclewson Linux - Security 1 06-29-2005 10:21 AM
entry in fstab file Tinku Linux - General 6 10-02-2004 01:06 AM


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