合并 *.csv文件并使用awk在终端中将文件名添加为一列

tpgth1q7  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(120)

摘要:我有近500个 *.csv文件,我需要合并成一个csv文件,在合并过程中,每个csv的文件名需要添加到新列的每行中。
我在这里读到了很多关于stackoverflow和其他的线程。我正在尝试在终端中做这个(不是我在终端中运行的脚本)。这里是我到目前为止所得到的。当我运行这个终端时,它返回“for quote〉”,并且没有完成。我希望有人能很容易地指导我。

for f in *.csv; do awk -f  ' { x=1 ; if ( x == NR ) { print "date,ProductNumber,Brand,Description,Size,UnitType,Pack,UPC,Available,Status,Delivery Due Date"  } else { gsub(".csv","",FILENAME); print FILENAME","$0 }  }  “$f”  >  “output$f”; done

每个csv文件的结构都相同,下面是一些示例数据:

ProductNumber,Brand,Description,Size,UnitType,Pack,UPC,Available,Status,Delivery Due Date
="0100503","BARNEY BUTTER","ALMOND BTR,SMOOTH","16 OZ   ","CS","   6",="0094922553584","    99","Active","          "
="0100701","NATRALIA","BODY LOTION,DRY SKIN","8.45 FZ ","EA","   1",="0835787000765","    33","Active","          "
="0101741","SAN PELLEGRINO","SPRKLNG BEV,ARANCIATA,ROS","6/11.15F","CS","   4",="0041508300360","     0","Active","          "
kwvwclae

kwvwclae1#

awk -v OFS=, '
    FNR == 1 {
        print "date,ProductNumber,Brand,Description,Size,UnitType,Pack,UPC,Available,Status,Delivery Due Date"
        file = FILENAME
        sub(/.csv$/, "", file)
    }
    {print file, $0}
' *.csv > out.csv

如果列表中的文件太长,则

find . -name '*.csv' -print0 | xargs -0 awk '...' > out.csv
velaa5lx

velaa5lx2#

awk -v OFS=, '
    NR == 1 && FNR == 1 {
        file = FILENAME
        sub(/.csv$/, "", file)
        print "filename", $0
    }
    NR > 1 && FNR > 1{
        file = FILENAME
        sub(/.csv$/, "", file)
        print file, $0
    }
' *.csv > out.csv

这对我很有效。保留第一个文件的第一行/头,并为保存文件名的列添加一个头,然后转储所有文件。

相关问题