AWK - syntax error: unexpected end of file

大兔子大兔子 提交于 2021-01-29 05:13:22

问题


I have about 300 files in a folder, trying to remove comma in CSV, when I run in the loop I got an error

MY CODE :

#!/bin/bash
FILES=/home/whoisdat/all-data/*
{

for f in $FILES
do  

{
awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i) } 1' $f > allan-$f
}
done

Error:

root@s1.allheartweb.com [all-data]# sh /home/all-data/unique.sh
/home/whoisdat/all-data/unique.sh: line 12: syntax error: unexpected end of file

回答1:


The right way to do what you're doing is:

awk -F'"' -v OFS='' '
    FNR==1 { close(out); out="allan-"FILENAME }
    { for (i=2; i<=NF; i+=2) gsub(/,/, "", $i); print > out }
' /home/whoisdat/all-data/*

We close the previous output file when we start reading the next input file to avoid a "too many open files" error from most awks when we get past a limit of a dozen or so (GNU awk which can handle many open files suffers a slowdown instead which is also undesirable), and we only close it there instead of once per input line processed to avoid the significant performance hit of opening and closing output files once per input line.

The above assumes you aren't running the command under /home/whoisdat/all-data/ and so creating allan-* files in /home/whoisdat/all-data/ while the script is running.




回答2:


awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i);print $0 >> ("allan-"FILENAME);close("allan-"FILENAME) }' /home/whoisdat/all-data/* 

There is no need to loop on the files, just allow awk to process all the files and use FILENAME to track the files being processed.



来源:https://stackoverflow.com/questions/65810000/awk-syntax-error-unexpected-end-of-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!