gpt4 book ai didi

linux - 如何为每条记录添加标题到行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:25 25 4
gpt4 key购买 nike

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

我已将其转置为行,但我需要向行添加标题。

例子

输入文件

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

有效:)

最佳答案

使用 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';' ...
...
...

关于linux - 如何为每条记录添加标题到行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com