AWK-语法错误:文件意外结束 [英] AWK - syntax error: unexpected end of file

查看:64
本文介绍了AWK-语法错误:文件意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中大约有300个文件,试图删除CSV中的逗号,当我在循环中运行时出现错误

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

我的代码:

#!/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

错误:

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='' '
    FNR==1 { close(out); out="allan-"FILENAME }
    { for (i=2; i<=NF; i+=2) gsub(/,/, "", $i); print > out }
' /home/whoisdat/all-data/*

当我们开始读取下一个输入文件时,我们将关闭前一个输出文件,以避免出现打开文件过多"的情况.当超过10个左右的限制时,大多数awks都会产生错误(可以处理许多打开文件的GNU awk会变慢,这也是不希望的),我们只在那儿关闭它,而不是对每个输入行进行一次处理,以避免每行输入一次打开和关闭输出文件的性能显着提高.

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.

以上假设您未在/home/whoisdat/all-data/下运行命令,因此在脚本运行时在/home/whoisdat/all-data/中创建allan- *文件.

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.

这篇关于AWK-语法错误:文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆