sailaja kasanur
sailaja kasanur

Reputation: 87

using find command in unix to search for a newline

I would like to search all .java files which have the newline escape sequence \n (backslash followed by 'n') in the files.

I am using this command:

find . –name "*.java" –print | xargs grep “\n”

but the result shows all lines in .java files having the letter n. I want to search for a newline \n.

Can you please suggest a solution?

Example:

x.java
method abc{
String msg="\n Action not allowed.";}

y. java
method getMsg(){
String errMsg = "\n get is not allowed.";}

I want to search all *.java files having these type of strings defined with newline escape sequence.

Upvotes: 0

Views: 13102

Answers (3)

Mark Plotnick
Mark Plotnick

Reputation: 10261

It looks like you want to find lines containing the 2-character sequence \n. To do this, use grep -F, which treats the pattern as a fixed string rather than as a regular expression or escape sequence.

find . –name "*.java" –print | xargs grep -F "\n"

Upvotes: 1

janos
janos

Reputation: 124666

I believe you're looking for this:

find . –name "*.java" –exec grep -H '"[^"]*\n' {} \;

The -H flag is to show the name of the file when there was a pattern match. If that doesn't work for you:

find . –name "*.java" –print0 | xargs -0 grep '"[^"]*\n'

If xargs -0 doesn't work for you:

find . –name "*.java" –print | xargs grep '"[^"]*\n'

If grep doesn't work for you:

find . –name "*.java" –print | xargs egrep '"[^"]*\n'

I needed this last version in Solaris, in modern systems the first one should work.

Finally, not sure if the pattern covers all your corner cases.

Upvotes: 0

oyss
oyss

Reputation: 692

This -P grep will match a newline character. using '$'. Since each line in my file contains a newline ,it will match every line.

grep -P '$' 1.c

I don't know why you want to match a newline character in files.That is strange.

Upvotes: 0

Related Questions