gpt4 book ai didi

linux - 将新行更改为所选行的空格

转载 作者:太空宇宙 更新时间:2023-11-04 05:44:56 25 4
gpt4 key购买 nike

我有一个文件,其输入如下:

Application handle                         = 36768
ID of agent holding lock = 36433
Application handle = 36807
ID of agent holding lock = 53074
Application handle = 52994
Application handle = 36433
Application handle = 36581
ID of agent holding lock = 36580
Application handle = 36458

但我想要以下格式的输出:

Application handle                         = 36768  ID of agent holding lock                 = 36433
Application handle = 36807 ID of agent holding lock = 53074
Application handle = 52994
Application handle = 36433
Application handle = 36581 ID of agent holding lock = 36580
Application handle = 36458

最佳答案

这个 awk 的工作原理:

awk '$1 == "Application"{if (l) print l; l=$0; next}
{l=l $0; next} END {print l}' file

Application handle = 36768 ID of agent holding lock = 36433
Application handle = 36807 ID of agent holding lock = 53074
Application handle = 52994
Application handle = 36433
Application handle = 36581 ID of agent holding lock = 36580
Application handle = 36458

说明:

这个 awk 首先评估这个条件:

  • $1 == "Application" 检查一行是否以 "Application" 开头。如果条件匹配,那么它首先执行此操作
    • if (l) print l; 打印 l 的值 if l 不为 null
    • 然后设置l=$0,将变量l的值设置为$0(整行)
    • 最后调用 next 使 awk 移动到下一条记录
  • 然后,仅当 $1 != "Application"(要连接的行)时才计算第二个 {...}
  • 该 block 是{l=l $0; next} 将第二行附加到变量 l 调用 next
  • 这个循环一直持续到最后一行,最后
  • END {print l} 被调用以打印最后一行。

关于linux - 将新行更改为所选行的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20397131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com