awk - how to add loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - how to add loop?
# 1  
Old 08-19-2010
awk - how to add loop?

I have the following awk one-liners:
Code:
awk '{if  ($2==1) print 1, $1}' infile | awk '{sum+=$2}END{print 1, sum/NR}' > infile2
:
:
awk '{if  ($2==n) print 1, $1}' infile | awk '{sum+=$2}END{print n, sum/NR}' > infile2

How to add a loop for instance from n=1 to n=100 in order to compact above one-liners into single one.

Moderator's Comments:
Mod Comment Use code tags, ty.

Last edited by zaxxon; 08-19-2010 at 11:47 AM..
# 2  
Old 08-19-2010
Like this?
Code:
awk '{if ($2==NR) print 1, $1}' infile | awk '{sum+=$2}END{print NR, sum/NR}' > infile2

# 3  
Old 08-19-2010
This doesn't work. Where is the loop ? Smilie
# 4  
Old 08-19-2010
this?
Code:
awk ' BEGIN { while(ARGC<=100) ARGV[ARGC++]=ARGV[1]; c=0 } FNR==1 {++c} {if ($2==c) print 1, $1}' infile \
    | awk ' BEGIN { while(ARGC<=100) ARGV[ARGC++]=ARGV[1]; c=0} FNR==1 {++c} {sum+=$2}END{print c, sum/NR}' > infile2

# 5  
Old 08-19-2010
Code:
for i in $(seq 1 100); do awk "{if (\$2==$i) print 1,\$1}" infile | awk "{sum+=\$2}END{print $i,sum/NR}" >> infile2; done

# 6  
Old 08-19-2010
bartus11: How to run it ? Smilie
# 7  
Old 08-19-2010
The same way you ran those one-liners.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk within a for loop

Hello, I currently have managed to get an awk function working inside a for loop that allows me to combine two files based on their headings but what I have not been able to do is print the output to files with variable names. awk ' NR==FNR {a=$0; next} /^>/ {$0 = $0" "a;} ... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

2. Shell Programming and Scripting

awk for loop help

Hello, I have an input file that looks like so: 1 2 3 4 5 6 7 8 9 and I just want to print the first and third column (note: my actual file contains many many more fields so I don't want to use '{ print $NF }' for each field I want. I tried using: awk 'BEGIN {FS=" "} { for (i=1;... (13 Replies)
Discussion started by: Rabu
13 Replies

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

4. Shell Programming and Scripting

awk for loop

Hi All, i have three files inputfilehello1,my name is unix.com. hello1,I am awesome. Hope you know this,hello2!,wat else hello2! hello1.txtHi Friends Hi Folks Hi Well-Wishers hello2.txtHoney Sweety Darling Expected outputHi Friends,my name is unix.com. Hi Friends,I am awesome. Hope you... (4 Replies)
Discussion started by: Makarand Dodmis
4 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

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

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

8. Shell Programming and Scripting

awk - loop from a to z

Hello, I was wondering if it is possible to do a loop on letters rather than numbers with awk (gawk). Basically I used to do: echo "nothing" | gawk '{for(i=1;i<11;i++)print i}' But I would like to do something like that (which obviously does not work): echo "nothing" | gawk '{for(i in... (6 Replies)
Discussion started by: jolecanard
6 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. UNIX for Dummies Questions & Answers

for loop in awk?

I am new to unix and have pieced together two scripts that work independently. The first checks all the filesystems and reports which are running low on space. df -m | awk 'int($4) > 75 { print $1 " has only " $3 "mb free from a total of " $2 ", this filesystem is " $4 " full! \n" }... (1 Reply)
Discussion started by: Bdawk
1 Replies
Login or Register to Ask a Question