I've tested the following line in terminal and it works perfectly:
我在终端测试了以下一行,它完美无缺:
zcat file_2016_01_01.gz | awk 'BEGIN { FS= "|"}; {if ($1 =="START" && $7 == "1234") {flag=1}} flag ; {if ($1=="STOP"){flag=0}}'> exit.txt
but when I try to automate the extraction for several files in a bash script the output files are empty:
但是当我尝试在bash脚本中自动提取多个文件时,输出文件为空:
Updated Code: the for loops were commented for showing that it wasn't the problem
更新的代码:对for循环进行了评论,表明它不是问题所在
#!/bin/bash
REF="ref"
CODE="code"
REP="path_to_file"
#for m in 0{1..3}
#do
# for d in 0{1..9} {10..31};
# do
# DATE="2016_${m}_${d}"
DATE="2016_01_04"
EXIT=${REF}_${CODE}_${DATE}.gz
if [ -e $REP/file_$DATE.gz ]
then
zcat $REP/file_$DATE.gz | awk 'BEGIN { FS= "|"}; {if ($1 =="START" && $7 == "'"$CODE"'" && $18== "'"$REF"'") {flag=1}} flag ; {if ($1=="STOP"){flag=0}} ' > $EXIT
else
echo "File not found!"
fi
# done
#done
exit 0
could anybody help me? Thanks a lot!
有人能帮助我吗?非常感谢!
1 个解决方案
#1
2
Problem seems to be here:
问题似乎在这里:
DATE="2016_$m_$d"
Since _
is considered part of variable name it is effectively making it:
由于_被认为是变量名称的一部分,因此它实际上是:
DATE="2016_${m_}${d}"
Since you don't have a variable named $m_
, effectively you will be getting the value of "2016_$d"
in your DATE
variable.
由于您没有名为$ m_的变量,因此您将在DATE变量中获得“2016_ $ d”的值。
You can fix it by using:
您可以使用以下方法修复它:
DATE="2016_${m}_${d}"
Also recommended to check your script on shellcheck.net to find and fix other obvious mistakes.
还建议检查shellcheck.net上的脚本以查找并修复其他明显的错误。
#1
2
Problem seems to be here:
问题似乎在这里:
DATE="2016_$m_$d"
Since _
is considered part of variable name it is effectively making it:
由于_被认为是变量名称的一部分,因此它实际上是:
DATE="2016_${m_}${d}"
Since you don't have a variable named $m_
, effectively you will be getting the value of "2016_$d"
in your DATE
variable.
由于您没有名为$ m_的变量,因此您将在DATE变量中获得“2016_ $ d”的值。
You can fix it by using:
您可以使用以下方法修复它:
DATE="2016_${m}_${d}"
Also recommended to check your script on shellcheck.net to find and fix other obvious mistakes.
还建议检查shellcheck.net上的脚本以查找并修复其他明显的错误。