Linux 列出以句点或分号结尾的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12707921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list lines ending with a period or semicolon
提问by femchi
how can I write a command by using grep or egrep to display all the lines in a file that end with a semicolon “;” or a period “.” character.
如何使用 grep 或 egrep 编写命令以显示文件中以分号“;”结尾的所有行 或句号“.” 特点。
采纳答案by Lee Netherton
With grep
(and egrep
) a dollar sign ($
) matches the end of a line, and a carret (^
) matches the beginning of a line. So, for example, if you wanted to match lines containing exactly the word "fish" and no other characters you could use this:
使用grep
(and egrep
) 美元符号 ( $
) 匹配行尾,插入符号 ( ^
) 匹配行首。因此,例如,如果您想匹配完全包含单词“fish”而没有其他字符的行,您可以使用以下命令:
grep '^fish$'
It's important to use single quotes to wrap the search expression so that bash doesn't do anything funny to it.
使用单引号包裹搜索表达式很重要,这样 bash 就不会对它做任何有趣的事情。
So, to answer your question, you will need to use the search pattern '[.;]$'
to match either a .
or ;
character followed by an end of line character. I am using this as an example test file:
因此,要回答您的问题,您需要使用搜索模式'[.;]$'
来匹配后跟行尾字符的.
或;
字符。我将此用作示例测试文件:
$ cat testfile
one
two;
three.
four:
And here is the result:
结果如下:
$ grep '[.;]$' testfile
two;
three.
If you also want to allow whitespace at the end of the line, then use this pattern: '[.;][ \t]*$'
which will match with any number of spaces or tab characters after the .
or ;
.
如果您还想允许空格在该行的末尾,然后用这个模式:'[.;][ \t]*$'
将匹配后的任何数量的空格或制表符.
或;
。
回答by unwind
This should do it:
这应该这样做:
$ grep -E '(;|\.)$'
The -E
switch enables regular expression mode. The expression simply matches a line ending in either a semicolon or a period.
该-E
开关启用正则表达式模式。该表达式仅匹配以分号或句点结尾的行。
Note: I haven't tested this.
注意:我没有测试过这个。
回答by Lev Levitsky
Without -E
:
没有-E
:
grep '\.\|;$' filename
回答by Vijay
three ways:any of these worked for me.
三种方式:这些方式中的任何一种都对我有用。
one:
一:
grep '[\.;]$' your_file
two:
二:
awk '/\.$/ || /;$/' your_file
three:
三:
awk '/[\.;]$/' your_file
回答by Gilles Quenot
With sed :
使用 sed :
sed -rn '/(\.|;)$/p'
With awk
用awk
awk '/(\.|;)$/'