awk - While Loop Error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - While Loop Error
# 1  
Old 08-31-2010
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:
Code:
 
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:

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

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 Smilie:
line 10: syntax error near unexpected token `done'
line 10: `done'

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

Last edited by gc_sw; 02-11-2011 at 08:20 AM..
# 2  
Old 08-31-2010
you can use this one
Code:
 
nawk 'NR>1 {print "set\t" $1 "\tRncFeatureId\t" $2}'  alarmlogs.txt

This User Gave Thanks to malikshahid85 For This Post:
# 3  
Old 08-31-2010
Quote:
Originally Posted by 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:
Code:
 
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:

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

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 Smilie:
line 10: syntax error near unexpected token `done'
line 10: `done'

how can i fix this? thanks for all your assist. Smilie
Did you write the script with a windows editor?

You could use the oneliner of malikshahid85 anyway.
This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 08-31-2010
Try:
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} >> /home/gcag/moshell/training/qweasd.mos
  i=$(( $i + 1 ))
done

Alternatively:
Code:
#!/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:

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

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

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 08-31-2010
I don't use awk much .. so you can try this code below
Code:
# !/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


Moderator's Comments:
Mod Comment Please use code tags, thank you
This User Gave Thanks to patkun For This Post:
# 6  
Old 08-31-2010
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
Code:
nawk 'NR>1 {print "set\t" $1 "\tRncFeatureId\t" $2}'  alarmlogs.txt

but same manner happens.
# 7  
Old 08-31-2010
Quote:
Originally Posted by 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
Code:
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:
Code:
tr -d '\r' < dosfile > unixfile

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with For loop

Hi My Requirement is to take the sum of each column below is the input file. 1 2 3 4 1 2 3 4 1 2 3 4 Initial i was using below command to achieve my desired result. however this was adding the row and not column. i am not able understand why this is happening awk... (1 Reply)
Discussion started by: scriptor
1 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

For loop using awk

hey everybody. i'm tryiing the whole day to solve my problem. i have a file with 500 columns and about 20 rows. i just want to build a for-loop using awk, which tests in the first column a condition for each row, then does the command and next goes further to the next column. so, for example... (3 Replies)
Discussion started by: helpless
3 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Syntax error for awk in a loop

can some one please tell me what is the problem with my syntax:confused: I have 100 files in one folder 1. want to read each of the line by line 2. calculate their number of the words between the first word and the last word of each line 3. create file for each file with number of words... (8 Replies)
Discussion started by: A-V
8 Replies

7. Shell Programming and Scripting

awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk. '{nr=$2;f = $1} END{for (i=1;i<=f;i++) if (nr != i) print i, nr }' input1.csv >output1.csvinput1.csv 1 9 3 5 4 1 7 6 8 5 10 6 output1.csv > with the missing line number 6. 6 is... (5 Replies)
Discussion started by: sdf
5 Replies

8. Shell Programming and Scripting

using awk loop

Hi, Gurus, I have a requirement as following: source data: 103,8,25 I want to get 103 8 25 1_8 103 8 25 9_16 103 8 25 17_24 103 8 25 25_32 103 8 25 33_40 103 8 25 41_48 103 8 25 49_56 103 8 25 57_64 103 8 25 65_72 103 8 25 73_80 103 8 25 81_88 103 8 25 89_96 103 8 25 97_104 103 8... (5 Replies)
Discussion started by: ken002
5 Replies

9. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

10. Shell Programming and Scripting

Error Using an if Loop Within a While Loop

Hello All, I am having a problem with an “if loop” within a “while loop” in my Korn Shell program. The basic concept of the program is that it searches for the existence of a series of load files in a load directory, and once it finds one of these files, it begins the following process: · Creates... (4 Replies)
Discussion started by: jonesdk5
4 Replies
Login or Register to Ask a Question