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:
Updated Code: the for loops were commented for showing that it wasn't the problem
#!/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!
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.