taf*_*ela 8 python bash scripting parsing
我正在尝试解析测试文件.该文件具有以下格式的用户名,地址和电话:
Name: John Doe1
address : somewhere
phone: 123-123-1234
Name: John Doe2
address : somewhere
phone: 123-123-1233
Name: John Doe3
address : somewhere
phone: 123-123-1232
Run Code Online (Sandbox Code Playgroud)
仅适用于近1万用户:)我想要做的是将这些行转换为列,例如:
Name: John Doe1 address : somewhere phone: 123-123-1234
Name: John Doe2 address : somewhere phone: 123-123-1233
Name: John Doe3 address : somewhere phone: 123-123-1232
Run Code Online (Sandbox Code Playgroud)
我更愿意这样做,bash但是如果你知道如何在python中做到这一点也很棒,那么拥有这些信息的文件在/ root/docs/information中.任何提示或帮助将不胜感激.
一种方式GNU awk:
awk 'BEGIN { FS="\n"; RS=""; OFS="\t\t" } { print $1, $2, $3 }' file.txt
Run Code Online (Sandbox Code Playgroud)
结果:
Name: John Doe1 address : somewhere phone: 123-123-1234
Name: John Doe2 address : somewhere phone: 123-123-1233
Name: John Doe3 address : somewhere phone: 123-123-1232
Run Code Online (Sandbox Code Playgroud)
请注意,我已将输出文件separator(OFS)设置为两个制表符(\t\t).您可以将其更改为您喜欢的任何字符或字符集.HTH.