awk - While Loop Error

Hi everybody;

I have an important script and want to get data from text file. Here what I want to do:

  • I have a text file named "alarmlogs.txt". It has lines as following:
 
ID RNCFeatureID
10 5
11 10
13 4
15 2

the content of each line is seperated by TAB. i want to take each variable except first line (because it is title). 10 5 11 10 13 4 15 2, etc.. then I want to process each variable in a while loop. this is the code:

 
#!/bin/sh
numberoflines=$(wc -l /home/gcsw/training/alarmlogs.txt)
i=2
while [ $i -le $numberoflines ]
do
cellproxy=$(awk -F "\t" 'NR==${i}{print $1}' /home/gcsw/training/alarmlogs.txt)
ercparam=$(awk -F "\t" 'NR==${i}{print $2}' /home/gcsw/training/alarmlogs.txt)
echo set ${cellproxy} RncFeatureId ${ercparam} \n >> /home/gcsw/training/qweasd.mos
i=$(( $i + 1 ))
done
  • cellproxy gets the first colomn of the ith line. for example, for the first line it is: 10.

  • ercparam gets the second colomn of the ith line. for example, for the first line it is: 5.

and I want this loop to go on up to number of total lines in alarmlogs.txt. and write output code

 
set 10 RncFeatureId 5
set 11 RncFeatureId 10
set 13 RncFeatureId 4
set 15 RncFeatureId 2

to qweasd.mos seperated by a new line. but this is the error :mad::
line 10: syntax error near unexpected token `done'
line 10: `done'

how can i fix this? thanks for all your assist. :slight_smile:

you can use this one

 
nawk 'NR>1 {print "set\t" $1 "\tRncFeatureId\t" $2}'  alarmlogs.txt

Did you write the script with a windows editor?

You could use the oneliner of malikshahid85 anyway.

Try:

#!/bin/sh
numberoflines=$(wc -l < /home/gcag/moshell/training/alarmlogs.txt)
i=2
while [ $i -le $numberoflines ]
do
  cellproxy=$(awk -F "\t" 'NR=='${i}'{print $1}' /home/gcag/moshell/training/alarmlogs.txt)
  ercparam=$(awk -F "\t" 'NR=='${i}'{print $2}' /home/gcag/moshell/training/alarmlogs.txt)
  echo set ${cellproxy} RncFeatureId ${ercparam} >> /home/gcag/moshell/training/qweasd.mos
  i=$(( $i + 1 ))
done

Alternatively:

#!/bin/sh
while read cellproxy ercparam
do
  if [ $(( linenr+=1 )) -gt 1 ]; then
    echo "set ${cellproxy} RncFeatureId ${ercparam}"
  fi
done < /home/gcag/moshell/training/alarmlogs.txt > /home/gcag/moshell/training/qweasd.mos

or:

#!/bin/sh
{
read x
while read cellproxy ercparam
do
  echo "set ${cellproxy} RncFeatureId ${ercparam}"
done
} < /home/gcag/moshell/training/alarmlogs.txt > /home/gcag/moshell/training/qweasd.mos

Or:

awk 'NR>1 {print "set "$1" RncFeatureId "$2}' home/gcag/moshell/training/alarmlogs.txt > /home/gcag/moshell/training/qweasd.mos

I don't use awk much .. so you can try this code below

# !/usr/bin/sh

while read LINE
do

     a=`echo $LINE | cut -d " " -f1`
     b=`echo $LINE | cut -d " " -f2`
     echo "set $a RncFeatureId $b" >> file2.txt

done < file.txt

tail +2 file2.txt >> file3.txt
mv  file3.txt file2.txt

Franklin52,

yes i have write is via Cygwin. the problem is within the loop, i think. the error is because of it. i have tried

nawk 'NR>1 {print "set\t" $1 "\tRncFeatureId\t" $2}'  alarmlogs.txt

but same manner happens.

If you have used a windows editor you have to remove the carriage returns first with dos2unix or with:

tr -d '\r' < dosfile > unixfile

thanks all of your assist but this code has completely carried my whole job :slight_smile: thank you

awk 'NR>1 {print "set "$1" RncFeatureId "$2}' /home/gcag/training/alarmlogs.txt > /home/gcag/training/qweasd.mos