Grep -v (inverse matching)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep -v (inverse matching)
# 1  
Old 07-19-2013
Question Grep -v (inverse matching)

I am totally unexperienced in writing scripts of any kind.
I am working on Mac OS X and would like to run a shell script to find files in a directory that do not conform to a specific naming convention and print to a text file in the same directory.

For example, I have a folder called ScriptTest.
In this folder there are 6 jpg images that must all follow this naming convention:
6 digits(0-9), an underscore, 3 digits(0-9), an underscore, 1 digit(1-4) (ex. 1234567_123_1).
I need to output the names of the files that do not conform to this naming convention, to a text file in the same directory.

I've searched many different forums, but all of the samples I find, either don't produce the results I need, or fail altogether.
Most have used a combination of find or ls used in conjunction with grep and xargs. I still have yet to get one to work...

I do like to research and try to figure things out on my own, but I'm at a dead end here. Any help, would be greatly appreciated!
# 2  
Old 07-19-2013
Code:
ls | grep -v "^[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9]_[1-4]\..*$"

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-19-2013
Still not there...

Quote:
Originally Posted by Corona688
Code:
ls | grep -v "^[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9]_[1-4]\..*$"

Thank you for the prompt reply!
However, still not there yet.

Here is the original ls of the folder contents(6 files):
Code:
1234567_123_1.jpg
1234567_456_1.jpg
1234567_56_2.jpg
123456_123_2.jpg
9876543-654_1.jpg
9876543_654-1.jpg

This is there output of the script(should be 4 files):
Code:
1234567_123_1.jpg
1234567_456_1.jpg
1234567_56_2.jpg
9876543-654_1.jpg
9876543_654-1.jpg

As you can see, the first 2 follow the naming convention perfectly, and should not be included in the results. The next line reporting back is named improperly, and is correct for being listed as the 2nd set of numbers is not 3 digits long. The last 2 lines reporting back are also correct for being reported as there are dashes instead of underscores in each of these. Lastly, there is number that did not even report back, and it should have since it is missing a digit from the first set of 7. See original input files: "123456_123_2.jpg" should have also reported back.

To summarize, the 2 files that were correctly named should not have been output, but were. And, it also failed to output the file that was missing a digit from the first part of the name.

Any thoughts??
Again, thank you for the prompt reply...

Last edited by Franklin52; 07-23-2013 at 10:38 AM.. Reason: Please use code tags
# 4  
Old 07-19-2013
Your regex looks for 6 digits to match the begin of file name, as specified. Not 7. So the output is entirely correct as your files start with seven chars.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-19-2013
Quote:
Originally Posted by RudiC
Your regex looks for 6 digits to match the begin of file name, as specified. Not 7. So the output is entirely correct as your files start with seven chars.
Thanks Rudi! I totally missed my typo in the original post, it should have said 7 instead of 6.

I have added the 7th digit to the string and it produced the correct results!

Thanks for the extra set of eyes!

---------- Post updated at 05:37 PM ---------- Previous update was at 05:35 PM ----------

Quote:
Originally Posted by Corona688
Code:
ls | grep -v "^[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9]_[1-4]\..*$"

As pointed out by RudiC, I only designated that the first set of digits at 6 when it should have been 7. I have since added in the 7th to the string, and it worked perfectly!

Thank you for your help with this!!!
Much, much appreciated!!!


SmilieSmilie
This User Gave Thanks to j_alicea For This Post:
# 6  
Old 07-22-2013
Question followup...

Ok, now they are going to hand-off a folder with several subfolders, each containing jpeg images.

Can I search recursively and only pull the jpg names, not the directory names?

What can I add to the script to do so?


Thanks everyone!
# 7  
Old 07-22-2013
Try
Code:
ls -R | grep -ov ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching and where, using grep

May somebody can give me a hint. I want to find using the command "grep" a certain word or term in a foo.txt file. By using the following command grep -i word file1 > newfile4 it puts it into a new foo.txt-file, the n times it matches. Fine, it matches n times, but how could I specify where... (2 Replies)
Discussion started by: 1in10
2 Replies

2. Shell Programming and Scripting

Grep with ... matching more than 3 characters

I am trying to understand what the grep command in ubuntu is trying to do here. The contents of my test file is given below harsha@harsha-H67MA-USB3-B3:~/Documents$ cat data abcd efghi jklmno pqr stuv wxyz When I grep for 3 dots (...) without the parenthesis as follows I would expect the... (4 Replies)
Discussion started by: sreeharshasn
4 Replies

3. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Grep negative matching

is it possible to reverse the output of grep -o (9 Replies)
Discussion started by: squrcles
9 Replies

5. Shell Programming and Scripting

Grep the non-matching lines

Hi, I need to make a script to extract the number that are not in a file. Example: I have file-A that has 100000 (70000-799999) numbers. And a file-B with number that already are in the system. Now I need to know/get the numbers that are not in system. I was thinking something like this:... (5 Replies)
Discussion started by: AK47
5 Replies

6. UNIX for Dummies Questions & Answers

Issues while pattern matching using grep

Hi, I have a file f1 wi the following data f1.txt ======== Report ID Report Name ----------------------------------------------------------------- Post Requests : 2 Post successes : 2 ============================================= I need to search for the... (2 Replies)
Discussion started by: RP09
2 Replies

7. Shell Programming and Scripting

Matching text using grep

Hi folks... Relatively new to scripting, but really struggling with something that will no doubt be second nature to most people on here: Trying to get an exact match on $sub, where sub is an ip address. subnet () { clear while true do ... (18 Replies)
Discussion started by: CiCa
18 Replies

8. Shell Programming and Scripting

Inverse Grep

Hi, I'm trying to wtite a script which actually print the text which doesn't contain a word , i mean to say. eg:- if a file contains the follwoing data Hello how ru ??? What ru doing ? what is the % of data contained ??? I want to write a script such that it prints the line excluding... (1 Reply)
Discussion started by: nagios
1 Replies

9. Shell Programming and Scripting

Matching white space through Grep

Hello All, I am trying to match white space in patterns through - Grep I tried ] & ] but none of them worked. Then I tried Perl extension '\s' and it worked. So just wanted to know if ] & ] are still supported or have they become deprecated. However they have been mentioned in the... (3 Replies)
Discussion started by: paragkalra
3 Replies

10. UNIX for Advanced & Expert Users

Grep Line with Matching Fields

Below is the scenario. Help is appreciated. File1: ( 500,000 lines ) : Three fields comma delimited : Not sorted 1234FAA,435612,88975 1224FAB,12345,212356 File2: ( 4,000,000 lines ) : Six fields comma delimited (Last 3 field should match the 3 fields of File1) : Not Sorted : ... (13 Replies)
Discussion started by: hemangjani
13 Replies
Login or Register to Ask a Question