Visit Our UNIX and Linux User Community


General find /grep question: files with lines ending in r


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers General find /grep question: files with lines ending in r
# 1  
Old 08-19-2013
General find /grep question: files with lines ending in r

I am trying to find files that have lines in them that end in an r. I have been able to locate files by using the following command:
Code:
find . -type f -name "*RECORDS"| xargs grep -l r$

However, I now want to find files that don't end in r anywhere. That means that no sentences or lines in those files should end with r. I am going around in circles, I have tried the following:

Code:
find . -type f -name "*RECORDS"| xargs grep -lv r$

This command returns files that have sentences that end in r but also those same files have sentences that don't end in r

I don't have the grep -L option and I have tried to use find . -type -f -name -not variations also bu it doesn't return the files.

The files it should return are only files that have sentences in them that end with r. I am going to then use these files as input for a script.
# 2  
Old 08-19-2013
You need a two step approach here: produce a list of all files (e.g. by placing tee into the pipe) and "r" files like you did before; then produce the difference between the two (e.g. grep -vf file1 file2).
# 3  
Old 08-19-2013
You can rewrite the previous
Code:
find . -type f -name "*RECORDS" -exec grep -l 'r$' {} \;

and this you can try to negate
Code:
find . -type f -name "*RECORDS" \! -exec grep -q 'r$' {} \; -print

# 4  
Old 08-19-2013
still returns files that have sentences that end and don't end in r

MadeinGermany:

I do not have the grep -q option but it looks like it is just a quiet option from online man page. The command you gave returns the file list, but again, all the files that have sentences that end in other letters as well as r are returned. I am trying to find the files that do not have ANY lines that end in r. Thanks for the help though, I will see if parts of that command are useful or if the tee command could help.
# 5  
Old 08-19-2013
Simple enough:

Code:
grep -v -l "r$" *

The -l lists filenames instead of lines.

The -v inverts the meaning -- instead of all files with any lines, all files with no lines.

These are both standard options which should work most anywhere.
# 6  
Old 08-19-2013
It's not that simple - grep -v -l "r$" * will suppress only those files that have ALL lines ending with "r".
# 7  
Old 08-19-2013
I see.

Code:
awk '{ A[FILENAME] } /r$/ { A[FILENAME++} END { for(X in A) if(!A[X]) print X }' *

This will not work on empty files.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #92
Difficulty: Easy
The Unix ls command can only lists files in the current directory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

2. Shell Programming and Scripting

How to find files containing two specific lines and delate those lines?

Hi I need to find files in a specified folder where are two specified lines of text and delate that lines. It looks like this" 35. ?>NL 36. <iframe>.......</iframe>NLThe problem is that "?>" is in the other lines and id should not be removed if the next line is not like "<iframe>....." So... (4 Replies)
Discussion started by: androwida
4 Replies

3. Shell Programming and Scripting

Find out if multiple files have lines ending with"r"

I am trying to find out which files in a group of files have lines ending in r. What I have is this: cat /tmp/*RECORDS| if grep r$>/dev/null; then echo "yes";else echo"no";fi Records is more than one file. There are the following files TEST-RECORDS /volume/testing /volume/programs ... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

5. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

6. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

7. Shell Programming and Scripting

Quick question on grep: grabbing lines above and below

Just a quick question on grep/egrep. I am writing a shell script that is looking for certain strings in a text file. It works well and gets exactly what I need. However, the way the program writes to the text file, it puts the timestamp in a line above the string I am looking for and the path... (3 Replies)
Discussion started by: thecoffeeguy
3 Replies

8. Shell Programming and Scripting

general question?

Perl, Python, and PHP are these languages easy to use? Are they command line or are they part of a GUI? (2 Replies)
Discussion started by: wmosley2
2 Replies

9. UNIX for Dummies Questions & Answers

grep something to find which files, lines contain something

in aix, i use grep something * to get the files, lines that contain something. but i couldnt get the same result from a sun box. what is the equivalent? thanks (4 Replies)
Discussion started by: yls177
4 Replies

10. UNIX for Dummies Questions & Answers

General Question

Hi, I've been racking my brains trying to remember, but, whats the command to change the default shell? I'm currently always in the Korn shell and I want to start out in the Bash shell. I'm running a variant of BSD I guess in Mac OS X 10.2.2 and Mandrake. Thanks. ccindyderek:confused: (4 Replies)
Discussion started by: ccindyderek
4 Replies

Featured Tech Videos