文本文件数据解析的线条,以及列的输出 [英] Text File data parsing lines and output as columns

查看:94
本文介绍了文本文件数据解析的线条,以及列的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析测试文件。该文件有用户名,地址和电话的格式如下:

I'm trying to parse a test file. the file has username, address and phone in the following format:

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

只有将近1万的用户:)我会做的是这些行转换为列,例如:

Only for almost 10k users: ) what I would like to do is convert those rows to columns, for example:

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

我想preFER做到在庆典但如果你知道怎么做了Python的将是巨大的,具有文件此信息在/根/文档/信息。任何提示或帮助将非常AP preciated。

I would prefer to do it in bash but if you know how to do it in python that would be great too, the file that has this information is in /root/docs/information. Any tips or help would be much appreciated.

推荐答案

GNU awk的一种方式

awk 'BEGIN { FS="\n"; RS=""; OFS="\t\t" } { print $1, $2, $3 }' file.txt

结果:

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

请注意,我已经设置输出文件分隔符( OFS )两个制表符( \\ t \\ t )。你可以改变这一切的性格或者设置你的字符,请。 HTH。

Note that, I've set the output file separator (OFS) to two tab characters (\t\t). You can change this to whatever character or set of characters you please. HTH.

这篇关于文本文件数据解析的线条,以及列的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆