AWK inside For loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK inside For loop
# 1  
Old 04-20-2011
Power AWK inside For loop

Hi,
Code:
awk -F"|" 'BEGIN{sum=0}{sum+=$2}END{printf("%d\n", sum)}' css.txt 
awk -F"|" 'BEGIN{sum=0}{sum+=$3}END{printf("%d\n", sum)}' css.txt 
awk -F"|" 'BEGIN{sum=0}{sum+=$4}END{printf("%d\n", sum)}' css.txt 
awk -F"|" 'BEGIN{sum=0}{sum+=$5}END{printf("%d\n", sum)}' css.txt 
awk -F"|" 'BEGIN{sum=0}{sum+=$6}END{printf("%d\n", sum)}' css.txt

The above code is working fine. Instead of using it, if I use the following for loop, nothing is getting printed. Please advice.
Code:
for i in 2 3 4 5 6  
do 
awk -F"|" 'BEGIN{sum=0}{sum+=$i}END{printf("%d\n", sum)}' css.txt 
done

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by thulasidharan2k; 04-20-2011 at 01:05 PM.. Reason: code tags, please!
# 2  
Old 04-20-2011
I can't imagine the above code would work 'fine'. It's all on one big line with no delimiters so would try to run it as one giant statement.

As for why the second doesn't work:
1) You missed an f in 'for'.
2) You missed several important ; or newlines.
3) The shell never substitutes anything inside '' quotes. awk provides a way to input variables instead.

Code:
for i in 2 3 4 5 6
do
        awk -v I=$i -F"|" 'BEGIN{sum=0}{sum+=$I}END{printf("%d\n", sum)}' css.txt
done

# 3  
Old 04-20-2011
You need some "special" quoting here:
Code:
for i in 2 3 4 5 6;  do awk -F"|" 'BEGIN{sum=0}{sum+='"$i"'}END{printf("%d\n", sum)}' css.txt; done

If you donīt quote the command correctly, the shell will not substitute the i variable inside the awk command, because nothing inside single quotes is substituted. Also, beware that there are missing semi-colons in your for loop structure.

Last edited by Franklin52; 04-20-2011 at 12:52 PM.. Reason: Please use code tags, thank you
# 4  
Old 04-20-2011
Hi All,

Thanks for your quick suggestions. The below codes are working...

Code:
for i in 2 3 4 5 6;  do awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css.txt; done

or
Code:
for i in 2 3 4 5 6;  do awk -F"|" 'BEGIN{sum=0}{sum=sum+$('$i')}END{printf("%d\n", sum)}' css.txt; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable and awk inside for loop

Thanks all for taking time out and reading this thread and big Thanks to all who have come forward for rescue. Background: I have a variable "nbrofcols" that has number of columns from a data file. Now, using this count in for loop, I am trying to get the maximum length of each column present... (7 Replies)
Discussion started by: svks1985
7 Replies

2. Shell Programming and Scripting

awk appending values inside a for loop

Hi i have a 2 files say test1 and test2 with the following data. cat file test test1 i want to append the output from a awk one liner to both the files. for i in cat file;do awk '/ whats happening is its printing the output properly. but not appending the way it showing in print... (1 Reply)
Discussion started by: venkitesh
1 Replies

3. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

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

5. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

6. Shell Programming and Scripting

For loop inside awk to read and print contents of files

Hello, I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for... (5 Replies)
Discussion started by: jaldo0805
5 Replies

7. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

8. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question