gpt4 book ai didi

shell - 如何从一个新添加的列中的第一行更改值

转载 作者:行者123 更新时间:2023-12-04 16:18:20 25 4
gpt4 key购买 nike

我有一个名为 file1.txt 的文件,其中包含以下数据

APPLICATION ODATE       IF_EXECUTING  APPLICATION_STATUS
Job1 20200731 Post NOTOK
Job2 20200731 Post NOTOK
Job3 20200731 Post NOTOK
JOb3 20200731 Post NOTOK
使用以下命令添加一个新列。
awk -v column=4 -v value="four" 'BEGIN {FS = OFS = "\t\t\t";}{for ( i = NF + 1; i > column; i-- ) {$i = $(i-1);}$i = value;print $0;}' file1.txt
输出:
APPLICATION ODATE       IF_EXECUTING  APPLICATION_STATUS                      four
Job1 20200731 Post NOTOK four
JOb2 20200731 Post NOTOK four
Job3 20200731 Post NOTOK four
JOb4 20200731 Post NOTOK four
这里添加了三个“\t”字符。第一行“四”应该与 APPLICATION_STATUS 隔开,只有一个\t,第一行“四”应该替换为“CYCLE_NUMER”
期望的输出:
APPLICATION ODATE       IF_EXECUTING  APPLICATION_STATUS    CYCLE_NUMBER
Job1 20200731 Post NOTOK four
JOb2 20200731 Post NOTOK four
Job3 20200731 Post NOTOK four
JOb4 20200731 Post NOTOK four

最佳答案

您能否尝试在 GNU awk 中使用所示示例进行以下、编写和测试? .

awk -v val="four" -v header="CYCLE_NUMBER" '
FNR==1{
print $0,header
next
}
{
print $0,val
}
' Input_file | column -t
或者根据@Cyrus 先生的评论,如果您想在 awk 内进行本身然后尝试跟随。
awk -v val="\t\t\tfour" -v header="\tCYCLE_NUMBER" '
FNR==1{
print $0,header
next
}
{
print $0,val
}
' Input_file
说明:为上述代码添加说明。第二个解决方案也更相似第一个解决方案只有第二个没有 column其中的命令和 TAB 打印在 awk 内得到处理变量本身。
awk -v val="four" -v header="CYCLE_NUMBER" '   ##Starting awk program from here with val value of four and header value of CYCLE_NUMBER here.
FNR==1{ ##Checking condition if FNR==1 then do following.
print $0,header ##Printing current line and header value here.
next ##next will skip all further statements from here.
}
{
print $0,val ##Printing current line with val here.
}
' Input_file | column -t ##Mentioning Input_file name here and sending output of awk command to column command to get it in good manner.

关于shell - 如何从一个新添加的列中的第一行更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63328460/

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