私たちは次のファイルを持っています:
cat info.txt
linux03.sys98.com net16777728 Speed: 1000Mb/s
linux03.sys98.com net16777728 Speed: 1000Mb/s
linux01.sys98.com net3f0 Speed: 1000Mb/s
linux01.sys98.com net3f0 Speed: 1000Mb/s
linux03.sys98.com net16777728 Duplex: Full
linux03.sys98.com net16777728 Duplex: Full
linux01.sys98.com net3f0 Duplex: Full
linux01.sys98.com net3f0 Duplex: Full
linux04.sys98.com net3f2 Link detected: no
linux04.sys98.com net3f3 Link detected: no
linux04.sys98.com net3f2 Speed: Unknown!
linux04.sys98.com net3f3 Speed: Unknown!
linux04.sys98.com net3f2 Duplex: Unknown! (255)
linux04.sys98.com net3f3 Duplex: Unknown! (255)
linux03.sys98.com net16777728 Link detected: yes
linux03.sys98.com net16777728 Link detected: yes
linux01.sys98.com net3f0 Link detected: yes
linux01.sys98.com net3f0 Link detected: yes
予想どおり、3 番目の単語を 2 番目の単語の先頭の 20 個のスペースに並べ替えようとしています。
linux03.sys98.com net16777728 Speed: 1000Mb/s
linux03.sys98.com net16777728 Speed: 1000Mb/s
linux01.sys98.com net3f0 Speed: 1000Mb/s
linux01.sys98.com net3f0 Speed: 1000Mb/s
linux03.sys98.com net16777728 Duplex: Full
linux03.sys98.com net16777728 Duplex: Full
linux01.sys98.com net3f0 Duplex: Full
linux01.sys98.com net3f0 Duplex: Full
linux04.sys98.com net3f2 Link detected: no
linux04.sys98.com net3f3 Link detected: no
linux04.sys98.com net3f2 Speed: Unknown!
linux04.sys98.com net3f3 Speed: Unknown!
linux04.sys98.com net3f2 Duplex: Unknown! (255)
linux04.sys98.com net3f3 Duplex: Unknown! (255)
linux03.sys98.com net16777728 Link detected: yes
linux03.sys98.com net16777728 Link detected: yes
linux01.sys98.com net3f0 Link detected: yes
linux01.sys98.com net3f0 Link detected: yes
printfや他のソリューションを使ってこれを行うにはどうすればよいですか?
ベストアンサー1
そしてperl
:
perl -ne 'printf "%s %-20s %s\n", /(\S+\s+)(\S+)\s*(.*)/' your-file
または、パターンと一致しない行に触れないでください。
perl -pe '$_ = sprintf "%s %-20s %s\n", $1, $2, $3
if /(\S+\s+)(\S+)\s*(.*)/' your-file