我有如下文件:
输入文件.csv:
"Input_File:",AB-ABCDE.20220128.02064600,,,,
"Date_Time Received:","1-28-22 3:47",,,,
1, abc, xyz,pq,11
2, def, xyz,rs,22
3, ghi, xyz,tu,33
4, jkl, xyz,vw,44
5, mno, xyz,xy,55
输出文件.csv:
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,1-28-22 3:47
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,1-28-22 3:47
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,1-28-22 3:47
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,1-28-22 3:47
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,1-28-22 3:47
我从命令获取文件名和日期时间awk
并存储如下:
file_name=`awk -F',' 'NR==1{print $2}' InputFile.csv`
date_time=`awk -F',' 'NR==2{print $2}' InputFile.csv`
我需要的是将捕获的 file_name 和 date_time 添加为每行的最后 2 列。
请帮我实现这个输出格式。
答案1
$ awk -F'"?,"?' -v OFS=',' 'NR<3{a[NR]=$2; next} {print $0, a[1], a[2]}' InputFile.csv
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,1-28-22 3:47
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,1-28-22 3:47
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,1-28-22 3:47
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,1-28-22 3:47
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,1-28-22 3:47
如果有人想打高尔夫球(受到现已删除的评论的启发),我们不需要删除 s "
:
$ awk -F, '{a[NR]=$2} NR>2{print $0,a[1],a[2]}' OFS=, InputFile.csv
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
答案2
啊啊,这两行有区别。尝试
awk -F, 'NR==1 {FN = $2} NR==2 {DT = $2} NR>2{print $0, FN, DT}' OFS=, InputFile.csv
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,"1-28-22 3:47"
答案3
您不需要将前两行的值捕获到 shell 变量中,因为awk
它将为您完成所有工作。
awk -F, '
BEGIN { OFS=FS } # Output separator is same as input separator
NR==1 { inputFile=$2 } # Input File
NR==2 { dtReceived=$2; gsub("\"", "", dtReceived) } # Date Received, without surrounding double quotes
NR>2 { print $0, inputFile, dtReceived } # Copy through every line appending two new fields
' InputFile.csv > OutputFile.csv
在这里,我们有一个四行awk
程序。您可以将所有内容放在一行中,但#
如果这样做,请删除注释部分。但是,请考虑当您在几周(或几个月)后再次阅读时,哪一个更具可读性。
awk -F, 'BEGIN{OFS=FS} NR==1{inputFile=$2} NR==2{dtReceived=$2;gsub("\"","",dtReceived)} NR>2{print $0,inputFile,dtReceived}' InputFile.csv > OutputFile.csv
请注意,如果这是 CSV 文件,则无需删除双引号。但由于您想要的输出文件省略了它们,我也在这里删除了它们。
输出
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,1-28-22 3:47
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,1-28-22 3:47
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,1-28-22 3:47
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,1-28-22 3:47
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,1-28-22 3:47
答案4
Python
#!/usr/bin/python
k=open('filename','r')
fin_lines=k.readlines()
firslin=fin_lines[0].strip().split(':')[-1]
firsli_conte=firslin.replace('"','').replace(",","")
secondlin=fin_lines[1].strip().split(",")[1].replace('"','')
for test in fin_lines[2:]:
print test.strip()+","+firsli_conte+","+secondlin
firs=$(awk -F ":" 'NR==1{gsub(/[",]/,"",$NF);print $NF}' filename )
sec=$(awk -F ":" 'NR==2{gsub(/[",]/,"",$2);gsub(/[",]/,"",$3);print $2":"$3}' filename )
awk -v firs="$firs" -v sec="$sec" 'NR>2{print $0","firs","sec}' filename
output
1, abc, xyz,pq,11,AB-ABCDE.20220128.02064600,1-28-22 3:47
2, def, xyz,rs,22,AB-ABCDE.20220128.02064600,1-28-22 3:47
3, ghi, xyz,tu,33,AB-ABCDE.20220128.02064600,1-28-22 3:47
4, jkl, xyz,vw,44,AB-ABCDE.20220128.02064600,1-28-22 3:47
5, mno, xyz,xy,55,AB-ABCDE.20220128.02064600,1-28-22 3:47