Grep语法条款

时间:2015-08-17 16:55:56

标签: regex awk grep clause

我正试图找到一种从电子书样本中查找语法条款的方法。 这是输入的样子:

This is a test my friend, this is just a test; I'm going to do some shopping:`what do you need?`
Nothing, he said.

期望的输出:

This is a test my friend
this is just a test
I'm going to do shopping
what do you need
Nothing
he said

关于如何实现这一目标的任何想法?

非常感谢!

3 个答案:

答案 0 :(得分:4)

您可以像这样使用gnu-awk:

awk -v RS='[\n.,;:`?]+' -v ORS='\n' '{$1=$1} 1' file
This is a test my friend
this is just a test
I'm going to do some shopping
what do you need
Nothing
he said

答案 1 :(得分:1)

这很接近:

grep -o '[[:alpha:][:space:]]\+' file

但是它翻译了#34;我' m"到换行符。鉴于您的示例标点符号,这有效:

grep -o '[^,;:`?.]\+' file

这会在标点字符后保留空格。要删除它,请将输出传递给

| sed 's/^ //'

答案 2 :(得分:0)

将它传递给tr。

cat input | tr ',' '\n'