Awk在第一行末尾添加管道

Awk在第一行末尾添加管道,awk,Awk,我的awk命令有点问题 目标是在我的CSV中添加一个新列: 标题必须是客户id 下一行必须是阵列中的客户id 这是我的csv: email|event_date|id|type|cha|external_id|name|date abcd@google.fr|2020-11-13 08:04:44|12|Invalid|Mail|disable|One|2020-11-13 dcab@google.fr|2020-11-13 08:04:44|13|Invalid|Mail|disable|Tw

我的awk命令有点问题

目标是在我的CSV中添加一个新列:

标题必须是客户id 下一行必须是阵列中的客户id 这是我的csv:

email|event_date|id|type|cha|external_id|name|date
abcd@google.fr|2020-11-13 08:04:44|12|Invalid|Mail|disable|One|2020-11-13
dcab@google.fr|2020-11-13 08:04:44|13|Invalid|Mail|disable|Two|2020-11-13
我希望得到以下输出:

email|event_date|id|type|cha|external_id|name|date|customer_id
abcd@google.fr|2020-11-13 08:04:44|12|Invalid|Mail|disable|One|2020-11-13|20200
dcab@google.fr|2020-11-13 08:04:44|13|Invalid|Mail|disable|Two|2020-11-13|20201
但当我做awk时,我得到了这样的结果:

awk-va=$echo${customerIdList[@]}'BEGIN{FS=OFS=|}FNR==1{$NF+1=customer_id}FNR>1{splita,b,}{print$0,b[NR-1]}'test.csv 电子邮件|事件|日期| id |类型| cha |外部| id |名称|日期|客户| id| abcd@google.fr|2020-11-13 08:04:44 | 12 |无效|邮件|禁用|一| 2020-11-13 | 20200 dcab@google.fr|2020-11-13 08:04:44 | 13 |无效|邮件|禁用|两| 2020-11-13 | 20201 其中customerIdList=20200 20201

客户id标题后面有一个管道,我不知道为什么:
有人能帮我吗?

请您尝试以下内容,并用展示的样品进行书写和测试

awk -v var="${customerIdList[*]}"  '
BEGIN{
  num=split(var,arr," ")
}
FNR==1{
  print $0"|customer_id"
  next
}
{
  $0=$0 (arr[FNR-1]?"|" arr[FNR-1]:"")
}
1
' Input_file
说明:增加对以上内容的详细说明

awk -v var="${customerIdList[*]}"  '      ##Starting awk program from here, creating var variable and passing array values to it.
BEGIN{                                    ##Starting BEGIN section of this program from here.
  num=split(var,arr," ")                  ##Splitting var into arr with space delimiter.
}
FNR==1{                                   ##Checking condition if this is first line.
  print $0"|customer_id"                  ##Then printing current line with string here.
  next                                    ##next will skip all further statements from here.
}
{
  $0=$0 (arr[FNR-1]?"|" arr[FNR-1]:"")    ##Checking condition if value of arr with current line number -1 is NOT NULL then add its value to current line with pipe else do nothing.
}
1                                         ##1 will print current line.
' Input_file                              ##Mentioning Input_file name here.

需要在awk中创建一个数组,因此将数组作为空格分隔的字符串传递,然后使用awk的split函数创建数组IdList。忽略标题NR>1,将行设置为行加上ListId数组NR-1的索引。

感谢您的回答,不幸的是,我只有第一个客户id 20200,第二行没有管道是空的通过添加${customerIdList[@]}我现在有awk:fatal:无法打开文件`BEGIN{num=splitvar,arr,}FNR==1{print$0 | customer|id next}{$0=$0 arr[FNR-1]?|arr[FNR-1]:}1'如果不读取这样的文件或目录,我肯定会复制正确的代码,也许我们没有相同的awk版本:GNU awk 4.0.2它正在与这个awk一起工作:awk-v IdList=${customerIdList[*]}BEGIN{OFS={splitIdList,ListId,}FNR==1}{$0=$0 | ListId[NR-1]}1'输入文件。csv@AChichi,确定不确定数组是如何创建的我在代码中使用了-v var=${customerIdList[@]}要将其放入var变量中,可以尝试-v var=${customerIdList[*]}一次尝试,然后让我知道?使用*:它似乎在没有标题customer_id的情况下工作,我将尝试将它添加到您的awkIt中使用这个:awk-v IdList=${customerIdList[*]}BEGIN{OFS=}{splitIdList,ListId,}FNR==1{$NF+1=customer_id}NR>1{$0=$0}ListId[NR-1]}1'
awk -v IdList="${customerIdList[*]}" 'BEGIN { split(IdList,ListId," ") } NR > 1 { $0=$0"|"ListId[NR-1]}1' file