如果字串為空,如何在替換期間在 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行有一個懸空。

{透過添加前綴來修復腳本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

然而,它沒有任何用處。做到這一點至少還有一個問題。

相關內容