Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 使用UNIX从字符内的句子中提取几个单词_Shell_Unix_Sed_Awk_Grep - Fatal编程技术网

Shell 使用UNIX从字符内的句子中提取几个单词

Shell 使用UNIX从字符内的句子中提取几个单词,shell,unix,sed,awk,grep,Shell,Unix,Sed,Awk,Grep,假设我从给定的行中提取第6列。你能告诉我如何提取“”之间的单词吗 请帮帮我。您可以使用grep-Po来实现此目的: $ grep -Po "(?<=')[^']*" <<< "ABC 123 HIJ 788sd78 XYZ I dont need this 'I just want this'" I just want this 例子 $cat a a b c d e ABC 123 HIJ 788sd78 XYZ我不需要这个“我只想要这个” a b c d e aaa

假设我从给定的行中提取第6列。你能告诉我如何提取“”之间的单词吗


请帮帮我。

您可以使用
grep-Po
来实现此目的:

$ grep -Po "(?<=')[^']*" <<< "ABC 123 HIJ 788sd78 XYZ I dont need this 'I just want this'"
I just want this
例子
$cat a
a b c d e ABC 123 HIJ 788sd78 XYZ我不需要这个“我只想要这个”
a b c d e aaa 123 HIJ 788sd78 XYZ我不需要这个“我只想要这个”

$awk-F“\t”'/ABC/{print$6}'a | grep-Po”(?假设您可以在不需要的文本中使用单引号,如您发布的示例输入所示(在
中):


请注意,需要使用
\047
或类似工具来表示命令行awk脚本调用的

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

切割-f 6

e、 g

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

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

你的行中有多少个
“…”
s?只存在于$6中?或者可能存在于其他列中?还有,
中的
”也不是你行的一部分吗?@user3251769这些答案对你有帮助吗?如果有,不要犹豫,把它们标记为已接受!没错,但这不是海报所要求的。好吧,如果他只是想要单引号中的东西在第六个字段中,grep ABC filename | cut-f 6 | cut-f2-d \'(原始海报没有100%清楚他想要什么),根据他发布的示例输入,它将打印
不需要这个
。我假设他的真实世界输入在他想要提取的数据之前的数据中没有。如果有,那么awk-f \{print$(NF-1)}”“但是你知道的。
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
$ 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**
grep ABC filename | cut -f 6
grep ABC filename | cut -f 6 | cut -f2 -d\'