Regular Expression in Date ls command


 
Thread Tools Search this Thread
Operating Systems Solaris Regular Expression in Date ls command
# 1  
Old 11-11-2013
Regular Expression in Date ls command

Hi,

I have got a problem in a regular expression with a file name containing date. I am using a regular to display the file in ls, but by using the same it gives me file name does not exist.

--## File exist when I do ls.
Code:
ls
amey_in20131018.csv

--## File name not showing when I use the regexp
Code:
ls -lrt amey_in[0-9]{8}.csv
amey_in[0-9]{8}.csv: No such file or directory

Can anybody please let me know what is wrong in my expression ?

Regards,
Amey

Last edited by Franklin52; 11-11-2013 at 05:55 AM.. Reason: Please use code tags
# 2  
Old 11-11-2013
Try piping output of ls command to grep. You also need to escape the curly brackets.
Code:
ls -l | grep 'amey_in[0-9]\{8\}.csv'

# 3  
Old 11-11-2013
It looks like mjf gave you a workaround for your problem, but didn't answer your question. In the command:
Code:
ls -lrt amey_in[0-9]{8}.csv

amey_in[0-9]{8}.csv is a shell pathname matching expression; not a regular expression. In shell pathname matching patterns, there is not way to specify a number of times to match the previous (sub)pattern. This pathname matching pattern would only match filenames that start with the string amey_in followed by a single decimal digit followed by the string {8}.csv.

PS Another way to do this would be to use:
Code:
ls -lrt amey_in[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].csv


Last edited by Don Cragun; 11-11-2013 at 06:28 AM.. Reason: Add postscript.
# 4  
Old 11-11-2013
Thanks Don,

So according to you there is no specific way to find number in times.


Regards,
Amey
# 5  
Old 11-11-2013
Quote:
Originally Posted by ameyrk
Thanks Don,

So according to you there is no specific way to find number in times.


Regards,
Amey
That is not at all what I said. I said that [0-9]{8} is not the way to look for eight decimal digits in a filename using shell pathname patterns. If you want to match eight decimal digits in a filename, mjf showed you a way to use ls and a regular expression in grep; and I showed you a way to use ls with a longer pathname matching pattern.
# 6  
Old 11-12-2013
Try this

Code:
ls -lrt | grep -E amey_in[0-9]{8}.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

Regular expression in grep command

Here is the content of a file: abcdefgh 1234 When I do: grep a?c <file> I expect the output to show "abcdefgh". Its not happening. Any ideas? "a?c" should mean either ac or c. This should mean the first line is a match. Yet its not happening. I have tried with -e option in grep, with... (1 Reply)
Discussion started by: Rameshck
1 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. Shell Programming and Scripting

Regular Expression in Find command [KSH]

Hello, I am trying to use regex wtih find command in KSH. For some reason it is not working as expected. Input: comm_000_abc_0102.c comm_000_abc.c 456_000_abc_1212.cpp 456_000_abc_.cpp Expected Output: comm_000_abc_0102.c kkm_000_abc_8888.cpp (Basically I want to find all... (6 Replies)
Discussion started by: vinay4889
6 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. Shell Programming and Scripting

Required help in perl regular expression substitution for this date format

Hi, I have written a small perl script to handle particular date format using perl, but it is not substituting the whole string. Can some one please check on what is the issue with the code. $_ = "Date: November 25, 2010 09:02:01 PM";... (1 Reply)
Discussion started by: sarbjit
1 Replies

7. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

8. Shell Programming and Scripting

validate date pattern using Regular Expression

Hi, i am java guy and new to unix. I want to validate date pattern using Regex expression here is the sample program i have written. #!/bin/sh checkDate="2010-04-09" regex="\\d{4}-\\d{2}-\\d{2}\$" echo $regex if ] then echo "OK" else echo "not OK" fi But the ouput is... (2 Replies)
Discussion started by: vvenu88
2 Replies

9. Shell Programming and Scripting

perl regular expression format date

Hi Everyone, Mon 18 Jan 2010 09:52:10 AM MYT the output is 20100118 09:52:10 how to do it in perl regular expression =~ Thanks (3 Replies)
Discussion started by: jimmy_y
3 Replies

10. UNIX for Dummies Questions & Answers

using regular expression for directories in find command

Hi, I want to find the files available in a directory /var/user/*/*/data/. I tried using the command "find /var/user/ -path '*/*/data/ -name '*' -type f" it says find: 0652-017 -path is not a valid option and then i tried using "find /var/user/ -name '*/*/data/*' -type f" but its not... (3 Replies)
Discussion started by: vinothbabu12
3 Replies
Login or Register to Ask a Question