简体   繁体   English

AWK - 语法错误:文件意外结束

[英]AWK - syntax error: unexpected end of file

I have about 300 files in a folder, trying to remove comma in CSV, when I run in the loop I got an error我在一个文件夹中有大约 300 个文件,试图删除 CSV 中的逗号,当我在循环中运行时出现错误

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
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.无需循环文件,只需让 awk 处理所有文件并使用 FILENAME 跟踪正在处理的文件。

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.当我们开始读取下一个输入文件时,我们关闭之前的 output 文件,以避免大多数 awk 在超过十几个左右的限制时出现“打开文件过多”错误(GNU awk,它可以处理许多打开的文件,速度会变慢相反,这也是不可取的),我们只在此处关闭它,而不是在每个输入行处理一次,以避免在每个输入行打开和关闭 output 文件对性能造成重大影响。

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.以上假设您没有在 /home/whoisdat/all-data/ 下运行命令,因此在脚本运行时在 /home/whoisdat/all-data/ 中创建 allan-* 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM