0

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.

5
  • \n is newline; \r is carriage return. What are you after? Dec 19, 2013 at 6:06
  • sry i was mistaken i am searching for new line \n Dec 19, 2013 at 6:26
  • On Unix systems, every line in every file ends with a newline, by definition. A line is a sequence of characters terminated by a newline. Even in a binary file (.class file or object file), lines are interpreted as a sequence of characters up to the next newline character (even if there are millions of characters in total, including zero or null bytes). Dec 19, 2013 at 6:47
  • @sailaja kasanur which shell you are using.
    – Chandru
    Dec 19, 2013 at 6:50
  • i am using tcsh shell Dec 19, 2013 at 8:03

3 Answers 3

1

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"
0
0

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.

2
  • I have many string messages defined as "\n xxx" in many .java files. These messages will be displayed during exceptions. I want to search all the strings messages which are defined starting with \n Dec 19, 2013 at 8:00
  • @sailajakasanur Then you should give us a exact example of the line you want to match.If your newline character is written in "\n".you can use grep -P "\\\n" e.g: echo "\n xxx"|grep -o -P "\\\n" to match "\n". sorry i don't use tcsh,don't know if it supports perl regex.
    – oyss
    Dec 19, 2013 at 8:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.