如果字符串为空,如何在替换期间在 for 循环中添加条件

如果字符串为空,如何在替换期间在 for 循环中添加条件

我试图在此代码中添加一个条件,例如,如果翻译文件中的 string 或 repl[string] 存在空字符串,则我的文件 input_chk.txt 具有以下详细信息:

输入_chk.txt

b73_chr10   w22_chr2
w22_chr7    w22_chr10
w22_chr8

代码 :

#!/usr/bin/awk -f
# Collect the translations from the first file.
NR==FNR { repl[$1]=$2; next }

# Step through the input file, replacing as required.
{
if 
for ( string in repl ) {
if (length(string)==0)
{
    echo "error"
}
else
{
sub(string, repl[string])
}
}
#if string is null-character,then we have to add rules,
#if repl[string] is null-character,then we have to delete rules or put # in front of all lines until we reach </rules> also
# And print.
1

# to run this script as $ ./bash_script.sh input_chk.txt file.conf

文件.conf

<rules>
<rule>
condition =between(b73_chr10,w22_chr1)
color = ylgn-9-seq-7
flow=continue
z=9
</rule>
<rule>
condition =between(w22_chr7,w22_chr2)
color = blue
flow=continue
z=10
</rule>
<rule>
condition =between(w22_chr8,w22_chr3)
color = vvdblue
flow=continue
z=11
</rule>
</rules>

但是,我的代码在第 8 行显示错误。如何包含条件,以便在第一列或第二列中缺少字符串时可以打印错误。

答案1

运行脚本发现问题:

  • 第 8 行是一个语法错误,这个词if本身就是一个。
  • 第 21 行是一个语法错误,这个词1本身就是一个语法错误。

将这些注释掉,第 6 行有一个悬空。{也许这是从某个工作脚本复制的,其中第 3 行有趣的记录收集语句在结论中进行处理。

{通过添加前缀来修复脚本END。将第 21 行更改1}.

现在(至少)脚本在语法上是正确的,并且没有错误。结果如下:

#!/usr/bin/awk -f
# Collect the translations from the first file.
NR==FNR { repl[$1]=$2; next }

# Step through the input file, replacing as required.
END {
#if 
for ( string in repl ) {
if (length(string)==0)
{
    echo "error"
}
else
{
sub(string, repl[string])
}
}
#if string is null-character,then we have to add rules,
#if repl[string] is null-character,then we have to delete rules or put # in front of all lines until we reach </rules> also
# And print.
}

# to run this script as $ ./bash_script.sh input_chk.txt file.conf

然而,它没有任何用处。做到这一点至少还有一个问题。

相关内容