LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep for a particular entry in the file name (https://www.linuxquestions.org/questions/linux-newbie-8/grep-for-a-particular-entry-in-the-file-name-721158/)

kais1 04-23-2009 09:51 AM

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

colucix 04-23-2009 10:09 AM

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

rabbit2345 04-23-2009 10:11 AM

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

Code:

ls -rlt | grep .-33

rabbit2345 04-23-2009 10:13 AM

Put a . in front of the dash.

ls -rlt | grep .-33

kais1 04-23-2009 10:19 AM

Quote:

Originally Posted by colucix (Post 3518428)
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

Libu 04-23-2009 10:21 AM

You don't have to use grep for this
Quote:

ls *-33*

kais1 04-23-2009 10:21 AM

Quote:

Originally Posted by rabbit2345 (Post 3518436)
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

colucix 04-23-2009 10:29 AM

Quote:

Originally Posted by kais1 (Post 3518444)
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
$


kais1 04-23-2009 01:31 PM

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

colucix 04-23-2009 03:43 PM

Quote:

Originally Posted by kais1 (Post 3518642)
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


All times are GMT -5. The time now is 08:57 AM.