简体   繁体   English

awk输出在bash中不起作用

[英]awk output not working inside bash

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! 非常感谢!

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上检查脚本,以查找并修复其他明显的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM