Using find and regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Using find and regular expressions
# 8  
Old 01-02-2013
How about
Code:
ls | grep '.*\.c$'


Last edited by kbw; 01-02-2013 at 04:46 AM..
# 9  
Old 01-02-2013
Quote:
Originally Posted by tinku981
Hi

Could you please advise how can one extract from the output of
find . -name "*.c" -print
only filenames in the current direcotry and not in its subdirectories?

I tried using (on Linux x86_64)
find . -name "*.c" -prune
but it is not giving correct output.

Whereas I am getting following output

./file1.c
./child_dir/file1.c
./child_dir/file4.c
./child_dir/file2.c
./child_dir/file3.c
./file4.c
./file2.c
./file3.c


Other than prune, is it possible to pipe GREP with find to get desired output?
If you don't want to descend into subdirectories, the simple way to do this is:
Code:
ls -1 *.c

Note that in the ls option -1 that is the digit one, not the letter ell.
Or, if you just want to see how pruning would work using a basic regular expression in grep instead of a filename pattern match in the shell, use:
Code:
ls|grep '[.]c$'

If you wonder why the $ is there, try running the above command with and without the $ in a directory in which there is a file named source.cpp and a file named source.c.

If you wonder why you need [.] instead of just . in a BRE, try the command above with and without the $ and with and without the square brackets in a directory that contains the files named before and files named abc, bcd, and c.
# 10  
Old 01-09-2013
Quote:
Originally Posted by PikK45
I think you can use "maxdepth" flag Smilie
Namely
Code:
find . -maxdepth 1 -name "*.c"

Unix find with a "." startdir:
Code:
find . \! -name . -prune -name "*.c" -print

grep:
Code:
\ls | grep '[.]c$'

If the number of files is not too high, you can try:
Code:
printf "%s\n" *.c

Code:
echo *.c


Last edited by MadeInGermany; 01-09-2013 at 04:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

2. UNIX for Dummies Questions & Answers

Regular Expressions -- Find spaces outside

Hello, I need help with using grep and regular expressions.... I have a long list of about 1000 lines of Chinese flashcards. Here's a small excerpt: 意文 yìwén (given name) 貴姓 guìxìng (honorable surname) 貴 guì (honorable) 姓 xìng (one's surname is; to be surnamed; surname) 呢 ne (interrogative... (2 Replies)
Discussion started by: arduino411
2 Replies

3. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

4. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

5. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

10. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies
Login or Register to Ask a Question