How to grep “\n” in file

2020-03-01 20:10发布

Sample file : abc.ksh

echo "This is a sample file." >> mno.txt
echo "\nThis line has new line char." >> mno.txt

I want

echo "\nThis line has new line char." >> mno.txt

as output.

标签: linux grep
5条回答
贼婆χ
2楼-- · 2020-03-01 20:13

Regular expression pattern search

grep -P '\n' mno.txt
查看更多
The star\"
3楼-- · 2020-03-01 20:21

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!.

查看更多
叛逆
4楼-- · 2020-03-01 20:25

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.)

查看更多
The star\"
5楼-- · 2020-03-01 20:29

You may try like this by escaping the backslash with another backslash:

grep '\\n' xyz.ksh
查看更多
聊天终结者
6楼-- · 2020-03-01 20:35

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
查看更多
登录 后发表回答