Output all files fitting the pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output all files fitting the pattern
# 1  
Old 07-26-2012
Output all files fitting the pattern

Hi there I need to create 1 line pipe that outputs list of files whose second character is digit in reverse order (file is a directory and called /tmp)the way I did
ls /tmp | grep '^.[[:digit:]]*' | sort -r
dunno it does not seem working. I tried to do it with another directory that I am sure contain a file fitting this BRE but it does not work.
So I have 2 question why it does not work (because file is a directory or perhaps because my pattern is written not correctly) and how should I do it ??? Thanks in advance!!! SmilieSmilie
# 2  
Old 07-26-2012
Code:
ls /tmp/?[0-9]* | sort -r

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-26-2012
Code:
ls ?[0-9]* | awk '{print substr($0,2,1), $0}' OFS='\t' | cut -f2-

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 07-26-2012
Quote:
Originally Posted by FUTURE_EINSTEIN
I need to create 1 line pipe that outputs list of files whose second character is digit in reverse order (file is a directory and called /tmp)the way I did
ls /tmp | grep '^.[[:digit:]]*' | sort -r
That pattern is incorrect. If the second character is any non-digit (or even if there is no second character at all), the pattern will match. * allows zero digits to match.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 07-26-2012
Quote:
Originally Posted by Corona688
Code:
ls /tmp/?[0-9]* | sort -r

Sorry I don't know why bur it does not working
ls: /tmp/?[0-9]*: No such file or directorySmilie

---------- Post updated at 01:19 PM ---------- Previous update was at 01:14 PM ----------

Quote:
Originally Posted by alister
That pattern is incorrect. If the second character is any non-digit (or even if there is no second character at all), the pattern will match. * allows zero digits to match.

Regards,
Alister
Thanks for the explanation but why "second character is any non-digit" I wanted digit instead I mean I have read that ^ means opposite but I thought it should mean opposite if it is inside []. So, you want to say that even though the it is inside single quote it means not????
# 6  
Old 07-26-2012
Quote:
Originally Posted by FUTURE_EINSTEIN
Sorry I don't know why bur it does not working
ls: /tmp/?[0-9]*: No such file or directorySmilie
Show us the entire script. That behavior would only occur if the argument to ls is quoted or if pathname expansion is disabled (very unlikely).

Quote:
Originally Posted by FUTURE_EINSTEIN
Thanks for the explanation but why "second character is any non-digit" I wanted digit instead I mean I have read that ^ means opposite but I thought it should mean opposite if it is inside []. So, you want to say that even though the it is inside single quote it means not????
It has nothing to do with ^. It has to do with *, which means to allow zero or more matches of the preceding character (in this case, any digit, [[:digit:]]). It's the zero part that's important. If the second character is not a digit, that's equivalent to zero digits. If there is no second character at all, that's also equivalent to zero digits (which is why your pattern matches a filename of just 1 character).

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 07-26-2012
Quote:
Originally Posted by alister
Show us the entire script. That behavior would only occur if the argument to ls is quoted or if pathname expansion is disabled (very unlikely).
It is not a script it is just a pipe
Code:
-bash-3.2$ ls /tmp/?[0-9]* | sort -r
ls: /tmp/?[0-9]*: No such file or directory


Quote:
It has nothing to do with ^. It has to do with *, which means to allow zero or more matches of the preceding character (in this case, any digit, [[:digit:]]). It's the zero part that's important. If the second character is not a digit, that's equivalent to zero digits. If there is no second character at all, that's also equivalent to zero digits (which is why your pattern matches a filename of just 1 character).

Regards,
Alister
Thanks again!
I get your point so is there a way to say for example I want first character to be anything second digit and whatever length of string ??? or I should just do it like '^.[[:digit:]]' like that???
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use awk to output such a pattern?

Hi all, I want to output the following pattern in a comma separted format, but am not able to do it. Table1.FIND.Value= 124 Data Data Data Data Data Table1.FIND.Value =256 Data Data Data Data Data The outPut i expect is (5 Replies)
Discussion started by: sidnow
5 Replies

2. Shell Programming and Scripting

Pattern output

Hi All, Seeking for your assistance on how to get the two pattern below. Example file1.txt GET /Sun/Cellular/version1/12 HTTP/1.0 100 Internal Error GET /Sun/Cellular/version1 GET /Sun/Cellular/version1 GET /Sun/Cellular/version1 GET /Sun/Cellular/version1/13 HTTP/1.0 100 Internal... (10 Replies)
Discussion started by: znesotomayor
10 Replies

3. UNIX Desktop Questions & Answers

How to add certain pattern to an output..?

Hi Everyone If i do this cat /usr/local/data/sales/taxware/indata/stepprod |wc -l i'll get output like this 5 And now i want to add a word for this output 'stepprod' (no quotes, only word) like below stepprod 5 Thank you (7 Replies)
Discussion started by: Ramanareddygv
7 Replies

4. Shell Programming and Scripting

Displaying log file pattern output in tabular form output

Hi All, I have result log file which looks like this (below): from the content need to consolidate the result and put it in tabular form 1). Intercomponents Checking Passed: All Server are passed. ====================================================================== 2). OS version Checking... (9 Replies)
Discussion started by: Optimus81
9 Replies

5. UNIX for Dummies Questions & Answers

Fitting a range of data

Hi, I am able to write an awk program that fits using the chi squared minimization method for each number is a data column... but I am wondering if it is possible to do that for a range of numbers at the same time. If I have a column for the observed data... and then say 10 columns for... (13 Replies)
Discussion started by: cosmologist
13 Replies

6. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

7. UNIX for Dummies Questions & Answers

Gnuplot - Histogram Fitting

Hi I am trying to fit my histogram data with a gaussian model and am encountering two problems: 1. I can't seem to fit the histogram data with a model fit y(x) 'bin.txt' using 2:xtic(1) via a,b,c (error: need 2 to 7 using specs) 2. Even when I manually guess the correct parameters for my fit and... (1 Reply)
Discussion started by: goffinj
1 Replies

8. UNIX for Dummies Questions & Answers

Fitting data in gnuplot with xyerrorbars

I was looking at the examples that show how to fit data using gnuplot (ex: gnuplot / misc (2E)) but I can't find a place that shows what to do if I have ranges for the x and y error bars. I tried the common sense: gnuplot> fit f(x) "data.txt" using 3:6:4:5:7:8 via a,b ... (8 Replies)
Discussion started by: cosmologist
8 Replies

9. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question