0

I am trying to add a condition in this code, that if there is null string in translation file for either string or repl[string] for example, my file input_chk.txt has following details:

input_chk.txt

b73_chr10   w22_chr2
w22_chr7    w22_chr10
w22_chr8

Code :

#!/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

file.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>

But,my code is showing error in line 8.How to include the condition so that it can print error if there is a string missing either in first column or second column.

1
  • if (string in repl) instead of if for (string in repl) will fix the error.
    – cuonglm
    Feb 15, 2016 at 1:52

1 Answer 1

1

Running the script shows the problems:

  • Line 8 is a syntax error, the word if by itself.
  • Line 21 is a syntax error, the word 1 by itself.

Commenting those out, there is a dangling { on line 6. Perhaps this was copied from some working script, where the interesting record-collecting statement on line 3 is processed on conclusion.

Fix the script by prefixing the { with END. Change the 1 on line 21 to a }.

Now (at least) the script is syntactically correct, and gives no errors. The result looks like this:

#!/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

However, it does nothing useful. Making it do that would be at least one more question.

1
  • how should the error be rectified.Add this condition before the assignment ?
    – shome
    Feb 15, 2016 at 1:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .