gc_sw
August 31, 2010, 4:29am
1
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 :
line 10: syntax error near unexpected token `done'
line 10: `done'
how can i fix this? thanks for all your assist.
you can use this one
nawk 'NR>1 {print "set\t" $1 "\tRncFeatureId\t" $2}' alarmlogs.txt
gc_sw:
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/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} \n >> /home/gcag/moshell/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 :
line 10: syntax error near unexpected token `done'
line 10: `done'
how can i fix this? thanks for all your assist.
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
patkun
August 31, 2010, 5:25am
5
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
gc_sw
August 31, 2010, 6:33am
6
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.
gc_sw:
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
gc_sw
August 31, 2010, 6:47am
8
thanks all of your assist but this code has completely carried my whole job thank you
awk 'NR>1 {print "set "$1" RncFeatureId "$2}' /home/gcag/training/alarmlogs.txt > /home/gcag/training/qweasd.mos