Please help on UNIX grep command for numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help on UNIX grep command for numbers
# 1  
Old 02-19-2016
Please help on UNIX grep command for numbers

I have a list of files like below, Do we have grep command to find files?

If i grep 03874 it should display the file 3874, Grep command should ignore 0 at the beginning. There could be more many leading 0's in filename.

Code:
$ ls -ltr
total 5
-rw-r--r-- 1 mqm mqm 15 Feb 19 17:07 4769
-rw-r--r-- 1 mqm mqm 16 Feb 19 17:08 05678
-rw-r--r-- 1 mqm mqm 15 Feb 19 17:09 3874
-rw-r--r-- 1 mqm mqm 16 Feb 19 17:09 2567
-rw-r--r-- 1 mqm mqm 16 Feb 19 17:54 09876


For example:
Code:
ls -ltr|grep 03874
-rw-r--r-- 1 mqm mqm 15 Feb 19 17:09 3874


Can you help me please?

Last edited by Scrutinizer; 02-19-2016 at 07:43 PM..
# 2  
Old 02-19-2016
This cannot be done with grep. You could try awk, for example:
Code:
awk -v s=05678 '$NF==s{sub($NF "$", $NF+0); print}'

# 3  
Old 02-19-2016
But in same directory i have files starting with 0 as well, When i compare 09876, then its displaying 9876, When i grep 09876 it should display 09876

for example:

Code:
ls -ltr|grep 09876
09876

Code:
$ ls -ltr
total 5
-rw-r--r-- 1 mqm mqm 15 Feb 19 17:07 4769
-rw-r--r-- 1 mqm mqm 16 Feb 19 17:08 05678
-rw-r--r-- 1 mqm mqm 15 Feb 19 17:09 3874
-rw-r--r-- 1 mqm mqm 16 Feb 19 17:09 2567
-rw-r--r-- 1 mam mqm 16 Feb 19 17:54 09876

Moderator's Comments:
Mod Comment If you continue to refuse to properly format your posts, you may be banned from this site.

Please use CODE tags (not color tags) when displaying code segments; and use CODE tags for sample input, sample output, AND code segments.

Last edited by Don Cragun; 02-19-2016 at 09:14 PM.. Reason: Change COLOR tags to CODE tags and add CODE tags.
# 4  
Old 02-19-2016
Hi prince1987,
Your request is a little ambiguous.

If you are saying that you want a script that will give you a long format list of files ending with a given string of digits where the given string may have an arbitrary number of leading zeros and the file (or files) may also have one or more leading zeros that are to be ignored when looking for files, you could try something like:
Code:
#!/bin/ksh
num=$1

while [ "$num" != "${num#0}" ]
do	num=${num#0}
done
ls -ltr | grep "0*$num$"

With the following files in my directory:
Code:
total 16
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 003874
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 03874
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 05678
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 09876
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 2567
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 3874
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 4769
-rw-r--r--  1 dwc  staff  856 Feb 19 16:49 problem
-rwxr-xr-x  1 doc  staff  105 Feb 19 16:53 tester

and the above code in the file named tester, any of the following commands:
Code:
./tester 3874
./tester 03874
./tester 00000000003874

produce the output:
Code:
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 3874
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 03874
-rw-r--r--  1 dwc  staff    0 Feb 19 16:44 003874

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep for a line containing only 5 numbers

How would you grep for a line containing only 5 numbers? Something like this. 10 2 12 1 13 (4 Replies)
Discussion started by: cokedude
4 Replies

2. UNIX for Dummies Questions & Answers

Grep for a range of numbers?

I am trying to extract specific information from a large *.sam file (it's originally 28Gb). I want to extract all lines that are on chr3 somewhere in the range of 112,937,439-113,437,438. Here is a sample line from my file so you can get a feel for what each line looks like: seq.4 0 ... (8 Replies)
Discussion started by: genGirl23
8 Replies

3. Shell Programming and Scripting

grep for a range of numbers

Dear Friends, I want to know how to grep for the lines that has a number between given range(start and end). I have tried the following sed command. sed -n -e '/20030101011442/,/20030101035519/p' However this requires both start and end to be part of the content being grepped. However... (4 Replies)
Discussion started by: tamil.pamaran
4 Replies

4. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

5. UNIX Desktop Questions & Answers

grep numbers

Hi all, I am new to unix and struggling to do the below I have few lines in a xml <title>abc:1</title> <description>abc:2</description> <language>abc:3</language> Is it possible to extract only the entire word like abc:1 abc:2 abc:3 instead of the entire line into a new file . Kindly... (3 Replies)
Discussion started by: umapearl
3 Replies

6. UNIX for Dummies Questions & Answers

Unix grep/test command

Hello, i have a script which checks if the user entered 8 numeric characters in the form of YYYYMMDD (birth date). If the user entered any non numeric characters, an error will be displayed: # Check to see if the 8 characters are all numbers # If not show error essage # And prompt user... (4 Replies)
Discussion started by: netmaster
4 Replies

7. UNIX for Dummies Questions & Answers

grep numbers

Hello, I'm trying to grep for digits surrounded by non digits and I'm obviously misinformed. Could someone help me get this sorted out here is what I have that is not working grep -ho '\D(\{11\})\D' *.txt (5 Replies)
Discussion started by: mcgrailm
5 Replies

8. Shell Programming and Scripting

unix grep command

I need to seach all strings that matches "if ; then" in all files If i put grep "if ; then" *.* it is not giving any result (1 Reply)
Discussion started by: pmsuper
1 Replies

9. Shell Programming and Scripting

grep for non numbers

Hi, I want to find out whether a string contains non numbers and + and - example : Str="0005000A" - It contains A Str="0005000+" - No problem What I have done is , echo $Str | grep I will have to list out all non numeric characters... (6 Replies)
Discussion started by: shihabvk
6 Replies

10. Shell Programming and Scripting

grep numbers range

I want to grep a range of numbers in a log file. My log file looks like this: 20050807070609Z;blah blah That is a combination of yr,month,date,hours,minutes,seconds. I want to search in the log file events that happened between a particular time. like between 20050807070000 to 20050822070000... (1 Reply)
Discussion started by: azmathshaikh
1 Replies
Login or Register to Ask a Question