将多个文件追加为一个 (awk)

  • 本文关键字:awk 一个 文件 追加 awk
  • 更新时间 :
  • 英文 :


我正在尝试将所有文件转储到一个文件中。为此,我使用了这个awk脚本(reading.awk(

BEGIN {
RS = "n"
filename= "DDL_dump.sql"
flag=1;
}
{
record =$0
if(record == ") ;")
flag = 0;
else if( flag==1)
print record >> filename 
}
END {
close(filename)
}

但是对于每个文件,它都会覆盖而不是追加。

尝试从与文件位于同一文件夹中的 bat 文件运行此命令。

FOR %%i IN (*.sql) DO awk -f reading.awk %%i

看起来你想要这个(没有外壳循环(:

awk 'FNR==1{f=1} /) ;/{f=0} f' *.sql > 'DDL_dump.sql'

多行形式的解释:

# True for the first line of each file
FNR==1 {
f=1 # Set f(lag)
}
# Once the pattern occurs ...
/) ;/ {
f=0 # ... reset the (f)lag
}
# As long as the f(lag) is true, print the line.
f

*.sql

是一个 shell glob 表达式,它扩展到当前文件夹中的所有.sql文件,并将它们作为参数传递给awk

> 'DDL_dump.sql'

是 shell 输出重定向,它将awk命令的输出存储到DDL_dump.sql

最新更新