使用UNIX从字符中的句子中取出几个单词

时间:2014-01-30 09:18:31

标签: shell unix sed awk grep

假设我从给定的行中提取Column-6。你能告诉我如何在''之间提取单词吗?

ABC 123 HIJ 788sd78 XYZ I don't need this '**I just want this**'. (Separated by tabs.)
grep ABC filename | awk -F"tab" '{print $6}' | __

请帮帮我。

3 个答案:

答案 0 :(得分:0)

您可以使用grep -Po

$ grep -Po "(?<=')[^']*" <<< "ABC 123 HIJ 788sd78 XYZ I dont need this 'I just want this'"
I just want this

你甚至可以将grep ABC filename | awk -F"tab" '{print $6}'压缩成类似的东西:

awk -F"\t" '/ABC/{print $6}' file | grep -Po "(?<=')[^']*"

实施例

$ cat a
a       b       c       d       e       ABC 123 HIJ 788sd78 XYZ I dont need this 'I just want this'
a       b       c       d       e       aaa 123 HIJ 788sd78 XYZ I dont need this 'I just want this'
$ awk -F"\t" '/ABC/{print $6}' a | grep -Po "(?<=')[^']*"
I just want this

答案 1 :(得分:0)

假设您可以在文本中包含您不想要的单引号,如发布的示例输入(don't)中所示:

$ cat file
ABC     123     HIJ     788sd78 XYZ     I don't need this '**I just want this**'.

$ awk -F'\t' '/ABC/{ sub(/\047[^\047]*$/,""); sub(/.*\047/,"") }1' file
**I just want this**

请注意,需要使用\047或类似代表'来命令行awk脚本调用。

答案 2 :(得分:0)

以下是从制表符分隔的标准输入中获取第六个字段的最简单方法:

cut -f 6

e.g。

grep ABC filename | cut -f 6

假设我们在第六个字段的引号字段之前或之内没有任何引号,并且引用的字段始终存在:

grep ABC filename | cut -f 6 | cut -f2 -d\'