我有一个以下格式的文本文件:
Name: John
Age: 25
Location: CA
Name: Mac
Age: 24
Location: CA
Name: Patrick
Age: 25
Location: CA
Name: Christian
Age: 25
Location: CA
我正在寻找帮助来使用Perl或awk转置数据。
Name Age Location
John 25 CA
Mac 24 CA
Patrick 25 CA
Christian 25 CA
如果有人能帮助我,我真的很感激。
答案 0 :(得分:2)
寻找这个?
kent$ awk -F'\n|: ' -v RS="" 'NR==1{print $1,$3,$5}{print $2,$4,$6}' file
Name Age Location
John 25 CA
Mac 24 CA
Patrick 25 CA
Christian 25 CA
如果你想让输出看起来更漂亮,将输出传递给|column -t
(值不包含空格)或在awk中使用printf
。
kent$ awk -F'\n|: ' -v RS="" 'NR==1{print $1,$3,$5}{print $2,$4,$6}' f|column -t
Name Age Location
John 25 CA
Mac 24 CA
Patrick 25 CA
Christian 25 CA
答案 1 :(得分:1)
perl -00 -nE 'say join"\t", /^(\w+)/mg if $. ==1;say join"\t", /(\w+)$/mg' file
输出
Name Age Location
John 25 CA
Mac 24 CA
Patrick 25 CA
Christian 25 CA
答案 2 :(得分:0)
使用awk
:
awk 'BEGIN {print "Name","Age","Location"} {print $2,$4,$6}' RS= file
Name Age Location
John 25 CA
Mac 24 CA
Patrick 25 CA
Christian 25 CA