如何在文件中grep“\ n”

时间:2022-03-02 13:56:20

Sample file : abc.ksh

示例文件: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.

4 个解决方案

#1


11  

Use -F to match fixed strings:

使用-F匹配固定字符串:

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

From man grep:

从男人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.)

将PATTERN解释为固定字符串列表,由换行符分隔,其中任何一个都要匹配。 (-F由POSIX指定。)

#2


4  

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:

只需用另一个反斜杠转义反斜杠并将正则表达式放在单引号中,这样shell就会将它传递给grep而不处理反斜杠本身:

grep '\\n' abc.ksh

#3


4  

Easiest way is using REGEX:

最简单的方法是使用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).

在REGEX语言中,$表示EOL(行尾),因此它通常匹配“\ n”(因为行尾很常见)。

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

警告:请小心使用支持REGEX!的grep版本。

#4


2  

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

您可以尝试使用另一个反斜杠转义反斜杠:

grep '\\n' xyz.ksh

#1


11  

Use -F to match fixed strings:

使用-F匹配固定字符串:

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

From man grep:

从男人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.)

将PATTERN解释为固定字符串列表,由换行符分隔,其中任何一个都要匹配。 (-F由POSIX指定。)

#2


4  

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:

只需用另一个反斜杠转义反斜杠并将正则表达式放在单引号中,这样shell就会将它传递给grep而不处理反斜杠本身:

grep '\\n' abc.ksh

#3


4  

Easiest way is using REGEX:

最简单的方法是使用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).

在REGEX语言中,$表示EOL(行尾),因此它通常匹配“\ n”(因为行尾很常见)。

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

警告:请小心使用支持REGEX!的grep版本。

#4


2  

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

您可以尝试使用另一个反斜杠转义反斜杠:

grep '\\n' xyz.ksh