linux + 文件输出操作

linux + 文件输出操作

我们有以下文件(或多或少可能是同一行)

more /tmp/info

linux01.sys76.com eno83122  Speed: 10000Mb/s
linux02.sys76.com eno83122  Speed: 10000Mb/s
linux03.sys76.com eno83122  Speed: 10000Mb/s
linux01.sys76.com eno83122 Speed: 10000Mb/s
linux02.sys76.com eno83122 Speed: 10000Mb/s
linux03.sys76.com eno83122 Speed: 10000Mb/s
linux01.sys76.com eno83122 Speed: 10000Mb/s
linux02.sys76.com eno83122 Speed: 10000Mb/s
linux03.sys76.com eno83122 Speed: 10000Mb/s
linux01.sys76.com eno83122  Duplex: Full
linux02.sys76.com eno83122  Duplex: Full
linux03.sys76.com eno83122  Duplex: Full
linux01.sys76.com eno83122 Duplex: Full
linux02.sys76.com eno83122 Duplex: Full
linux03.sys76.com eno83122 Duplex: Full
linux01.sys76.com eno83122 Duplex: Full
linux02.sys76.com eno83122 Duplex: Full
linux03.sys76.com eno83122 Duplex: Full
linux01.sys76.com eno83122  Link detected: yes
linux02.sys76.com eno83122  Link detected: yes
linux03.sys76.com eno83122  Link detected: yes
linux01.sys76.com eno83122 Link detected: yes
linux02.sys76.com eno83122 Link detected: yes
linux03.sys76.com eno83122 Link detected: yes
linux01.sys76.com eno83122 Link detected: yes
linux02.sys76.com eno83122 Link detected: yes
linux03.sys76.com eno83122 Link detected: yes

如何操纵输出以便我们得到以下结果

预期结果(带有行缩进)

linux01.sys76.com eno83122  Speed     10000Mb/s
linux02.sys76.com eno83122  Speed     10000Mb/s
linux03.sys76.com eno83122  Speed     10000Mb/s
linux01.sys76.com eno83122  Speed     10000Mb/s
linux02.sys76.com eno83122  Speed     10000Mb/s
linux03.sys76.com eno83122  Speed     10000Mb/s
linux01.sys76.com eno83122  Speed     10000Mb/s
linux02.sys76.com eno83122  Speed     10000Mb/s
linux03.sys76.com eno83122  Speed     10000Mb/s


linux01.sys76.com eno83122  Duplex        Full
linux02.sys76.com eno83122  Duplex        Full
linux03.sys76.com eno83122  Duplex        Full
linux01.sys76.com eno83122  Duplex        Full
linux02.sys76.com eno83122  Duplex        Full
linux03.sys76.com eno83122  Duplex        Full
linux01.sys76.com eno83122  Duplex        Full
linux02.sys76.com eno83122  Duplex        Full
linux03.sys76.com eno83122  Duplex        Full



linux01.sys76.com eno83122  Link_detected  yes
linux02.sys76.com eno83122  Link_detected  yes
linux03.sys76.com eno83122  Link_detected  yes
linux01.sys76.com eno83122  Link_detected  yes
linux02.sys76.com eno83122  Link_detected  yes
linux03.sys76.com eno83122  Link_detected  yes
linux01.sys76.com eno83122  Link_detected  yes
linux02.sys76.com eno83122  Link_detected  yes
linux03.sys76.com eno83122  Link_detected  yes

答案1

Awk解决方案:

awk 'prev && prev != $3 { print ORS ORS }{ prev = $3 }1' /tmp/info

输出:

linux01.sys76.com eno83122  Speed: 10000Mb/s
linux02.sys76.com eno83122  Speed: 10000Mb/s
linux03.sys76.com eno83122  Speed: 10000Mb/s
linux01.sys76.com eno83122 Speed: 10000Mb/s
linux02.sys76.com eno83122 Speed: 10000Mb/s
linux03.sys76.com eno83122 Speed: 10000Mb/s
linux01.sys76.com eno83122 Speed: 10000Mb/s
linux02.sys76.com eno83122 Speed: 10000Mb/s
linux03.sys76.com eno83122 Speed: 10000Mb/s



linux01.sys76.com eno83122  Duplex: Full
linux02.sys76.com eno83122  Duplex: Full
linux03.sys76.com eno83122  Duplex: Full
linux01.sys76.com eno83122 Duplex: Full
linux02.sys76.com eno83122 Duplex: Full
linux03.sys76.com eno83122 Duplex: Full
linux01.sys76.com eno83122 Duplex: Full
linux02.sys76.com eno83122 Duplex: Full
linux03.sys76.com eno83122 Duplex: Full



linux01.sys76.com eno83122  Link detected: yes
linux02.sys76.com eno83122  Link detected: yes
linux03.sys76.com eno83122  Link detected: yes
linux01.sys76.com eno83122 Link detected: yes
linux02.sys76.com eno83122 Link detected: yes
linux03.sys76.com eno83122 Link detected: yes
linux01.sys76.com eno83122 Link detected: yes
linux02.sys76.com eno83122 Link detected: yes
linux03.sys76.com eno83122 Link detected: yes

相关内容