完成后将变量保存在 .awk 文件中以备下次运行 [英] Save variable in .awk file after completion for next time it runs
问题描述
所以,我有一个 .awk 文件,它将一个订单文件拆分为多个订单文件.文件名从 orders.xml(包含 100 个订单的主文件)变为诸如 order-1.xml、order-2.xml、order-3.xml 等递增的文件.
So, I have a .awk file that will split one order file into many order files. The file name goes from orders.xml (main file containing say, 100 orders) into files that increment like order-1.xml, order-2.xml, order-3.xml etc.
.awk 文件拆分完成后,假设最后一个文件是 order-100.xml,如何保存该数字以备下次使用,那么当 .awk 文件再次运行时,它从 101 开始?所以它会去 order-101.xml, order-102.xml 等等.
When the .awk file is finished splitting the files, assume the last file is order-100.xml, how can I save that number for next time, so when the .awk file runs again, it begins from 101? so it will go order-101.xml, order-102.xml etc.
这是我当前的代码,但我不知道如何去做.
This is the current code I have but I can't figure out how to do it.
被递增的变量是'count'变量,它应用文件名
The variable being incremented is the 'count' variable, which applies the file name
script.awk
/<Order/ {
rfile="order-" count ".xml"
print $0 > rfile
getline
while ($0 !~ "<\/Order>" ) {
print > rfile
getline
}
print $0 > rfile
close(rfile)
count++
}
所以,总结一下,假设脚本在 order-65.xml 上完成,下次运行时,它应该从 66 开始.
So, to sum it up, say the script finishes on order-65.xml, the next time it runs, it should start from 66.
推荐答案
你需要的是这样的东西(因为你没有提供样本输入/输出来测试),所以你需要:
Something like this (untested since you haven't provided sample input/output to test against) is what you need:
#!/usr/bin/env bash
outdir='/some/dir' # set this to whatever directory path you want to create the files in.
lastNr=$(printf '%s\n' "$outdir"/order-*.xml | sed 's/.*-\([^.]*\).*/\1/' | sort -n | tail -1)
awk -v count="$lastNr" -v outdir="$outdir" '
/<Order/ {
close(rfile)
rfile = outdir "/order-" (++count) ".xml"
}
{ print > rfile }
' "${@:--}"
您询问了如何在对 awk 脚本的调用之间保存 count
的值,但您已经将其保存为您正在创建的文件名称的一部分,因此上面的 shell 脚本只是在调用 awk 之前将该值读回 lastNr
.我给它取了一个与 count
不同的名字,部分是为了明确什么是 shell 变量 (lastNr
) 和 awk 变量 (count
).
You asked how to save the value of count
between calls to the awk script but you're already saving that as part of the names of the files you're creating so the above shell script just reads that value back into lastNr
before calling awk. I gave it a different name from count
partially to make it clear what's a shell variable (lastNr
) vs an awk variable (count
).
将其保存在名为 splitOrders
之类的文件中,使其可执行,然后将其作为 ./splitOrders orders.xml
Save that in a file called something like splitOrders
, make it executable, and then execute it as ./splitOrders orders.xml
请注意,splitOrders
是一个 shell 脚本,而不是 awk 脚本.它在内部调用 awk 来解释 awk 脚本,但它也调用其他命令(printf、sed、sort 和 tail).并不是说 shell 脚本只是 Unix 中命令的一种实现,你只是根据命令的作用来命名命令,而不是它们所用的语言,所以你不用给它一个 .sh
或任何其他类型的语言/工具相关后缀.
Note that splitOrders
is a shell script, not an awk script. Internally it calls awk to interpret an awk script but it calls other commands (printf, sed, sort, and tail) too. Not also that a shell script is just one implementation of a command in Unix and you just name commands based on what they DO, not the language they're written in, so you don't give it a .sh
or any other kind of language/tool-dependent suffix.
这篇关于完成后将变量保存在 .awk 文件中以备下次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!