awk - 解析引号内的字符串
问题描述
我有一个日志文件,其中包含以下模式的行。我想提取引号中的两个字符串并将它们写入另一个文件,每个文件都在一个单独的列中。(并非所有行都具有这种模式,但这些特定行是按顺序排列的。)
输入
(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
解决方案
使用单引号作为字段分隔符应该可以让您大部分时间到达那里,然后您必须有一种方法来唯一地标识您想要匹配的行。以下适用于您提供的示例,但可能必须根据我们没有看到的文件中的行进行调整。
$ awk -v q="'" 'BEGIN {FS=q; OFS="\t"} /Warning: Variants/ && NF==5 {print $2, $4}' file
Variant47911 Variant47910
exm2254099 exm12471
newrs140234726 exm15862
推荐阅读
- c# - 如何在招摇中设置自定义属性类型
- java - 用户输入,例如 (1,34 * 10) 而不是 13,4 抛出异常,有没有办法让用户在控制台上输入浮动点数
- django - 子域没有发送任何数据 | ERR_EMPTY_RESPONSE | Heroku
- java - 将 Spring Boot Web 和 Batch 应用程序打包在一起以进行 PCF 部署
- c# - 对类型“httpsessionstatebase”的引用声称它在“system.web”中定义,但找不到
- python - 将 Python 脚本转换为 C++
- checksum - 如何计算校验和(适用于 DFPlayer mini)
- google-apps-script - 单元格中的 getDisplayValue() 和 getValue() 返回 #VALUE!大多数时候出错
- c# - 如何使用带有 MVC5 表单的 Dropzone 指定回发行为
- docker - 由于缺少 chown 权限,DHIS2 核心 docker 容器重新启动