awk - 打印所有行,但连接匹配模式的连续行
问题描述
我有一个包含以下数据的文件:
;Citation1 begins here and contains characters including , . and numbers
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
;Citation2 begins here and contains characters including , . and numbers but
;this one continues on the next line
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
我想将所有行打印到一个新文件中。但是,当连续的行以相同的字符(此处为“;”)开头时,我想将它们连接到同一行。因此,上述输入文件将显示为:
;Citation1 begins here and contains characters including , . and numbers
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
;Citation2 begins here and contains characters including , . and numbers but this one continues on the next line
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
我尝试过使用不同的 awk 命令变体,例如:
awk '/;/ && last {printf "%s","\n"last;$0}{printf "%s",$0}END{print} /;/{last=$0}' input.txt > output.txt
但一直没有成功。
解决方案
$ awk '
{
curr = $0
printf "%s%s", ( (prev ~ /^;/) && sub(/^;/,"") ? OFS : ors ), $0
ors = ORS
prev = curr
}
END { print "" }
' file
;Citation1 begins here and contains characters including , . and numbers
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
;Citation2 begins here and contains characters including , . and numbers but this one continues on the next line
DATA 1 259.85 101000 0.094837707 0.9089 /
2 266.07 101000 0.097842938 0.8997 /
3 270.95 101000 0.105071894 0.8899 /
4 273.35 101000 0.112016587 0.8849 /
5 278.75 101000 0.134569045 0.87 /
推荐阅读
- python - 为什么用于展平 n 维可迭代列表的“for for”习语有效?
- android - 键盘只允许底页打开一半
- python - 在 python pandas 中合并数据的问题
- webpack - Webpack:将 JS 转换为 SASS 和 CSS
- c++ - C++ 内联构造函数和符号表
- javascript - 如何通过点击自动推进项目?
- java - 根据嵌套字段对 JSON 数据进行排序
- python - 类方法不会从另一个模块相互调用
- github-actions - Github Actions:如何在脚本中使用策略/矩阵
- flutter - 如何获取运行 Flutter Web 应用程序的设备的唯一标识符?