How to grep "\n" in file

Use -F to match fixed strings:

$ grep -F "\n" file
echo "\nThis line has new line char." >> mno.txt

From man grep:

-F, --fixed-strings

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.)


Easiest way is using REGEX:

grep "$" filename  # this will match all lines ending with "\n" (often all lines)
grep "PATTERN$"    # this will match all lines ending with "PATTERN\n"

In REGEX language, $ means EOL (end of line), so it will often match "\n" (cause is very common as the end of line).

WARNING: be careful to use versions of grep that support REGEX!.


Simply escape the backslash with another backslash and put the regex in single quotes so the shell does pass it to grep without handling the backslashes itself:

grep '\\n' abc.ksh

Tags:

Linux

Grep