首页 > 解决方案 > 解析引号内的字符串

问题描述

我有一个日志文件,其中包含以下模式的行。我想提取引号中的两个字符串并将它们写入另一个文件,每个文件都在一个单独的列中。(并非所有行都具有这种模式,但这些特定行是按顺序排列的。)

输入

(multiple lines of header)
Of these, 0 are new, while 1723332 are present in the base dataset.
Warning: Variants 'Variant47911' and 'Variant47910' have the same position. 
Warning: Variants 'exm2254099' and 'exm12471' have the same position.
Warning: Variants 'newrs140234726' and 'exm15862' have the same position.

期望的输出:

Variant47911     Variant47910
exm2254099       exm12471
newrs140234726   exm15862 

这会检索行但不知道如何指定需要打印的字符串。

awk '/Warning: Variants '*'/ Input 

标签: awksed

解决方案


使用单引号作为字段分隔符应该可以让您大部分时间到达那里,然后您必须有一种方法来唯一地标识您想要匹配的行。以下适用于您提供的示例,但可能必须根据我们没有看到的文件中的行进行调整。

$ awk -v q="'" 'BEGIN {FS=q; OFS="\t"} /Warning: Variants/ && NF==5 {print $2, $4}' file
Variant47911    Variant47910
exm2254099      exm12471
newrs140234726  exm15862

推荐阅读