Linux 如何为每条记录向行添加标题

Linux 如何为每条记录向行添加标题,linux,bash,awk,gawk,Linux,Bash,Awk,Gawk,我有一个大文件,列之间用“;”分隔 我已经将其转置到行中,但我需要向行中添加标题 范例 输入文件 31561;49215;10;1196825801480000 31561;49215;12;1196825801480000 31561;48665;14;1196825806980000 我使用此代码将列转换为行 sed '$'!'G' file | tr ';' '\n' 我得到的结果是: 31561 49215 10 1196825801480000 31561 49215 12 11

我有一个大文件,列之间用“;”分隔

我已经将其转置到行中,但我需要向行中添加标题

范例

输入文件

31561;49215;10;1196825801480000
31561;49215;12;1196825801480000
31561;48665;14;1196825806980000
我使用此代码将列转换为行

sed '$'!'G' file | tr ';' '\n'
我得到的结果是:

31561
49215
10
1196825801480000

31561
49215
12
1196825801480000

31561
48665
14
1196825806980000
但我想添加标题,得到如下内容:

Status1
Information1
Line     : 31561
Point    : 49215
Code:    : 10
TB      :  1196825801480000

Status1
Information1
Line     : 31561
Point    : 49215
Code:    : 12
TB      :  1196825801480000

Status1
Information1
Line     : 31561
Point    : 48665
Code:    : 14
TB      :  1196825806980000
awk -F';' '{
   print "Status1"
   print "Information1"
   print "Line     :" $1;
   print "Point    :" $2; 
   print "Code     :" $3; 
   print "TB      :" $4"\n"; 
}' file
请帮我解决这个问题。谢谢

我更改代码如下:

Status1
Information1
Line     : 31561
Point    : 49215
Code:    : 10
TB      :  1196825801480000

Status1
Information1
Line     : 31561
Point    : 49215
Code:    : 12
TB      :  1196825801480000

Status1
Information1
Line     : 31561
Point    : 48665
Code:    : 14
TB      :  1196825806980000
awk -F';' '{
   print "Status1"
   print "Information1"
   print "Line     :" $1;
   print "Point    :" $2; 
   print "Code     :" $3; 
   print "TB      :" $4"\n"; 
}' file

它的工作原理是:)

使用awk的解决方案:

awk -F';'  '{
   if ( NR != 1 ) printf "\n";
   print "Status1\nInformation1";
   print "Line: " $1;
   print "Point: " $2;
   print "Code: " $3;
   print "TB:", $4;
}' file
要使用就地编辑,请执行以下操作:

awk -i inplace -F';' ...
...
...

使用awk的解决方案:

awk -F';'  '{
   if ( NR != 1 ) printf "\n";
   print "Status1\nInformation1";
   print "Line: " $1;
   print "Point: " $2;
   print "Code: " $3;
   print "TB:", $4;
}' file
要使用就地编辑,请执行以下操作:

awk -i inplace -F';' ...
...
...

使用awk

$ awk -F";" 'BEGIN{ split("Line;Point;Code;TB",array)} {print "Status1" ORS "Information 1"; for(i=1; i<=NF; i++) print array[i]"     : "$i; printf ORS}' file
Status1
Information 1
Line     : 31561
Point     : 49215
Code     : 10
TB     : 1196825801480000

Status1
Information 1
Line     : 31561
Point     : 49215
Code     : 12
TB     : 1196825801480000

Status1
Information 1
Line     : 31561
Point     : 48665
Code     : 14
TB     : 1196825806980000

使用awk

$ awk -F";" 'BEGIN{ split("Line;Point;Code;TB",array)} {print "Status1" ORS "Information 1"; for(i=1; i<=NF; i++) print array[i]"     : "$i; printf ORS}' file
Status1
Information 1
Line     : 31561
Point     : 49215
Code     : 10
TB     : 1196825801480000

Status1
Information 1
Line     : 31561
Point     : 49215
Code     : 12
TB     : 1196825801480000

Status1
Information 1
Line     : 31561
Point     : 48665
Code     : 14
TB     : 1196825806980000

很好,PesaThe,只要我修改所需的输出,是否有可能更新您的代码,是否有可能直接使用文件而不是在从列到行的转换之后进行更新。awk-i替代了什么?请你解释一下。我可能误解了你的评论
-i inplace
在位编辑文件。因此,更改不会打印在标准输出上,而是直接打印到文件中。感谢PesaThe我理解,我以前使用过它,只是使用了sed-i@user2734334是的,我误解了你的评论,认为你也想这么做:)太好了,佩萨瑟,我只需要修改所需的输出,是否有可能更新你的代码,有没有可能直接使用文件,而不是在从列到行的转换之后执行此操作。awk-i替代了什么?请你解释一下。我可能误解了你的评论
-i inplace
在位编辑文件。因此,更改不会打印在标准输出上,而是直接打印到文件中。感谢PesaThe我理解,我以前使用过它,只是使用了sed-i@user2734334是的,我误解了你的评论,以为你也想这么做:)蝙蝠侠,是否有可能将标题和数据精确地分开,就像我想要的输出和不使用的输出一样\t@user2734334:是的,只需在print语句中包含您所需的确切空格数,而不是像updated answer.batMan中那样在print语句中包含
\t
,是否有可能将标题和数据精确地分开,就像我想要的输出和不使用的输出一样\t@user2734334:是的,只需在print语句中包含您所需的确切空格数,而不是像更新的答案中那样在print语句中包含
\t