Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

grep specific number from a list of numbers

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-07-2012
godzilla07 godzilla07 is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 9 September 2013, 11:44 PM EDT
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
grep specific number from a list of numbers

Hello. I have 9060 files labelled File1 to File9060. They are in numerical order. When I grep a file eg. File90 it will show me all files that contain the pattern "File90", eg File901 or File9001. I can only get specific files for File1000 or higher. How can I resolve this problem? Is there a command for the specific pattern recognition only?
Sponsored Links
    #2  
Old Unix and Linux 03-07-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 May 2015, 2:49 PM EDT
Location: Saskatchewan
Posts: 20,649
Thanks: 894
Thanked 3,653 Times in 3,416 Posts
Please post exactly what you're doing, word for word, letter for letter, keystroke for keystroke. I can't tell what you're doing so don't know why it's going wrong.

You can force grep to print filenames with the -l(filename only) or -H(filename: matching line) options.
Sponsored Links
    #3  
Old Unix and Linux 03-07-2012
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 May 2015, 3:03 PM EDT
Location: NM
Posts: 10,451
Thanks: 336
Thanked 860 Times in 799 Posts
Yes, There are several ways

First off ls will match patterns so you do not have to use grep.

Code:
ls File90

has no errors like that.

However, since you want grep, use a trailing $:

Code:
ls | grep 'File90$'

If you want files numbered 1000 > 9999:

Code:
ls | grep 'File[0-9]{4}$'

This gives you any file that has exactly 4 numbers in the last 4 places in the file name.
    #4  
Old Unix and Linux 03-07-2012
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 May 2015, 12:48 AM EDT
Location: India
Posts: 1,886
Thanks: 27
Thanked 539 Times in 516 Posts
Quote:
Originally Posted by jim mcnamara View Post
If you want files numbered 1000 > 9999:

Code:
ls | grep 'File[0-9]{4}$'

Reminder:

Code:
man grep
    In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning;


Code:
ls | egrep 'File[0-9]{4}$'

Sponsored Links
    #5  
Old Unix and Linux 03-09-2012
dodasajan dodasajan is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 3 June 2014, 7:29 AM EDT
Posts: 48
Thanks: 0
Thanked 1 Time in 1 Post
for grep a particular file u can use:

ls |grep "file90"
Sponsored Links
    #6  
Old Unix and Linux 03-09-2012
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 14 April 2015, 8:02 PM EDT
Posts: 6,398
Thanks: 288
Thanked 675 Times in 644 Posts
@dodasajan
I refer you to post #1 . Your code repeats the original problem but also with a typing error in the name of the file.


Code:
touch File90
touch File900
touch File9000

ls File90
File90

ls | grep 'File90'
File90
File900
File9000

ls |grep 'File90$'
File90

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep the specific string or user's list from the file rohit22hamirpur Programming 1 07-25-2011 12:11 AM
the smallest number from 90% of highest numbers from all numbers in file Apfik Shell Programming and Scripting 11 05-22-2011 10:54 PM
iterate through list of numbers and print specific lines with awk euval Shell Programming and Scripting 4 09-17-2010 09:47 AM
list all files containing 4 digit number using grep hobiwhenuknowme UNIX for Dummies Questions & Answers 5 08-11-2010 01:10 AM
Getting unique list of numbers using grep tinku UNIX for Dummies Questions & Answers 6 08-12-2009 07:19 AM



All times are GMT -4. The time now is 04:38 PM.