Awk command in while loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Awk command in while loop
# 1  
Old 05-26-2010
Awk command in while loop

Hello!

I've got a loop in which I am processing a list of values gotten through a file with read command.

It seems that instead of processing the lines (values) one by one, I process them all together.

the input file is:

Code:
 
20
20
20
80
70
70
20

The code is:

Code:
 
#!/bin/sh
 
while read -r Line
do
        STR5="$(awk 'BEGIN {FS = ","} ; {print $1}')"
        if [ "$STR5" = "20" ]; then
                echo "CHOICE A" >> SEN5$1.txt
        elif [ "$STR5" = "70" ]; then
                echo "CHOICE B" >> SEN5$1.txt
        elif [ "$STR5" = "80" ]; then
                echo "CHOICE C" >> SEN5$1.txt
        else
                echo "$STR5" >> SEN5$1.txt
        fi
        echo "$STR5" >> SEN5$1.txt
done < input.txt

and produces the same output as the input file, meaning that the last 'else' statement is validated each time. By testing I have seen that it takes the whole file (all lines) in each iteration.


I have also tested this code:

Code:
 
while read -r Line
do
awk 'BEGIN {FS = ","} ; { if ($1 = 20) print "CHOICE A"; else if ($1 = 70) print "CHOICE B"; else if ($1 = 80) print "CHOICE C" ; else print $1}' >> SEN5$1.txt
done < input.txt

which returns

Code:
 
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A
CHOICE A

which propably is due to checking with the 1st value each time.


Any ideas?
# 2  
Old 05-26-2010
How about ...

Code:
#!/bin/bash

cat in.file | while read LINE
do
  case $LINE in
    "20") echo "Choice A" >> out.file ;;
    "70") echo "Choice B" >> out.file ;;
    "80") echo "Choice C" >> out.file ;;
    *   ) echo "Crapshot" >> out.file ;;
  esac
done

exit 0
#finis

Code:
[house@leonov] bash test.bash
[house@leonov] cat out.file
Choice A
Choice A
Choice A
Choice C
Choice B
Choice B
Choice A

This User Gave Thanks to dr.house For This Post:
# 3  
Old 05-26-2010
Doh!

Using the $Line itself, dunno why it escaped me this easily! :embarassed:


Thanks a lot Dr Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command with a loop

Dear all, I would be grateful for your help with the following. I have the following file (file.txt), which is about 10,000 lines long: ID1 ID2 0 1 0.5 0.6 ID3 ID4 0 0 0.4 0.8 ID1 ID5 0 1 0.5 0.3 ID6 ID2 1 0 0.4 0.8 The IDs in the first two columns can occur between... (12 Replies)
Discussion started by: aberg
12 Replies

2. UNIX for Dummies Questions & Answers

Loop awk command on files in a folder

Hi, I'd like to loop an action over all files with given extension within a folder. The "main" action is: awk -F "\t" 'BEGIN{OFS="\t"}{if ($10=="S") print$0; }' input.txt > output.txt The input.txt should be every file in the folder with *.subVCF extension; and the output should be a file... (3 Replies)
Discussion started by: dovah
3 Replies

3. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

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

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

6. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

7. Shell Programming and Scripting

For loop, awk command issue

limit.csv data -------------- 5600050 38Nhava 400077 27Bomay rate.txt data ------------- 38NhaVA 27BomaY 27Bomay below is my script: for i in `cat limit.csv` do b=`awk '{print $1}' $i` (4 Replies)
Discussion started by: p_satyambabu
4 Replies

8. Shell Programming and Scripting

Problem Using If & For loop in AWK Command

I am parsing file for the fields using awk command, first i check 26th field for two characters using substr function if it matches then using for loop on array i search 184th field for 4 chars if it matches then i print the required fields but on execution i get the error, please help...... (5 Replies)
Discussion started by: siramitsharma
5 Replies

9. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

10. 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
Login or Register to Ask a Question