awk output error while loop through array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk output error while loop through array
# 1  
Old 05-07-2012
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.

Code:
'{nr[$1]=$2;f = $1} END{for (i=1;i<=f;i++) if (nr[$1] != i)  print i, nr[i] }' input1.csv >output1.csv

input1.csv
Code:
1 9
3 5
4 1
7 6
8 5
10 6

output1.csv > with the missing line number 6. 6 is $NF of last line on input1.csv
Code:
1 9
2 
3 5
4 1
5 
7 6
8 5
9 
10 6


Last edited by Scrutinizer; 05-07-2012 at 09:10 AM.. Reason: code tags instead of html tags
# 2  
Old 05-07-2012
The script is working fine but its if (nr[$1] != i) test in the END clause is more than dubious.
You are explicitly excluding the line(s) which second field is equal to the second field of the last line read, here 10 6

Last edited by jlliagre; 05-07-2012 at 10:01 AM.. Reason: grammar
# 3  
Old 05-07-2012
Quote:
Originally Posted by jlliagre
The script is working fine but its if (nr[$1] != i) test in the END clause is more than dubious.
You are explicitly excluding the line(s) which second field is equal to the second field of the last read line, here 10 6
The script reads the first field of last line with f=$1 and not f=$NF. How can it be fixed?Smilie
# 4  
Old 05-07-2012
It uses nr[$1], which is $NF of the last line. What are you trying to achieve?


--
As an aside: $1 happens to be available because your particular awk retains that value in the END section. With some awks this value will be "", since the field variable are only available in the middle section.
# 5  
Old 05-07-2012
The test is pointless, just remove it:
Code:
'{nr[$1]=$2;f = $1} END{for (i=1;i<=f;i++) print i, nr[i] }'

This User Gave Thanks to jlliagre For This Post:
# 6  
Old 05-07-2012
Quote:
Originally Posted by Scrutinizer
It uses f=NR[$1], which is $NF of the last line. What are you trying to achieve?
A Blank line shall be inserted where $2 is blank, while $1 shall be the NR. This is what I want to achieve:

input.csv
Code:
1 9
3 5
4 1
7 6
8 5
10 6

ouptut.csv
Code:
1 9
2 
3 5
4 1
5 
6 
7 6
8 5
9 
10 6

---------- Post updated at 03:07 PM ---------- Previous update was at 03:04 PM ----------

Quote:
Originally Posted by jlliagre
The test is pointless, just remove it:
Code:
'{nr[$1]=$2;f = $1} END{for (i=1;i<=f;i++) print i, nr[i] }'

Thanks, wanted more than was needed!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

awk script: loop through array

I have a large file where I want to extract the data by using awk script. I have made a small sample of the input data. I have in the awk script two condition . The first one is to collect the initial time and the second one to collect the end time. I stored the difference between (Time=end-start)... (8 Replies)
Discussion started by: ENG_MOHD
8 Replies

3. Shell Programming and Scripting

Sorting output of AWK array

I need help to sort the output of an awk array Example datadata="1 blue 2 green 3 blue 4 yellow 5 blue 6 red 7 yellow 8 red 9 yellow 10 yellow 11 green 12 orange 13 black" My awk line to get output in one lineecho "$data" | awk ' {arr++; next} END { for (i in arr) { if(arr>1 )... (2 Replies)
Discussion started by: Jotne
2 Replies

4. Shell Programming and Scripting

Need to have output of AWK array in one line

I have this code echo $logfile | awk ' {arr++; next} END { for (i in arr) {print i} }' that gives me this output result1 result2 result3 I try to figure out how to get it like this result1 result2 result3 (4 Replies)
Discussion started by: Jotne
4 Replies

5. Shell Programming and Scripting

Array output through a for loop problematic with multiple elements.

This code works perfect when using a machine with only one interface online. (Excluding the loopback of course) But when I have other interface up for vmware or a vpn the output gets mixed up. I know I had this working when I was just reading ip's from files so I know it is not a problem with... (8 Replies)
Discussion started by: Azrael
8 Replies

6. Shell Programming and Scripting

Sorting awk array output?

Hi all, I have a script which produces a nice table but I want to sort it on column 3. This is the output line in the script: # Output { FS = ":"; format = "%11s %6s %-16s\n"; prinft "\n" printf ( format, "Size","Count","Who" ) } for (i in... (21 Replies)
Discussion started by: Cowardly
21 Replies

7. UNIX for Dummies Questions & Answers

Loop on array variable returning error: 0 not found

I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong. #!/usr/bin/ksh set -A MtPtArray /u03 /u06 tUbound=${#MtPtArray } echo $tUbound i=0 while ($i -lt $tUbound) do print... (4 Replies)
Discussion started by: KME
4 Replies

8. Shell Programming and Scripting

Help with awk in array in while loop

Hi everyone:) I have 2 files - IN & OUT. Example: IN A:13:30 B:45:40 . . . UNLIMITED OUT Z:12:24 Y:20:15 . . . UNLIMITED I want first row of numbers of IN - OUT. Example 13-12 45-20 My code is (2 Replies)
Discussion started by: vincyoxy
2 Replies

9. Shell Programming and Scripting

bash:awk output into an array

Hi, I have a file 1:apple orange:one 2:banana:two 3:cherry:3 When I do awk -F: ' { print $2 } ' file apple orange banana cherry Now, when i redirect awk output to the file it has issue with strings #!/bin/bash FILEA=file A=(`awk -F: ' { print $2 } ' $FILEA `) echo ${A} (2 Replies)
Discussion started by: phamp008
2 Replies

10. Shell Programming and Scripting

move output of awk to array

Hi experts, I have a the following awk command, awk '{print $1}' /users/jon/list4.txt. The output is 123 787 888 ... ... I want to move the output to array using shell programming. My shell is tcsh. Is it possible to move to array using shell porg? I know its possible in... (14 Replies)
Discussion started by: amitrajvarma
14 Replies
Login or Register to Ask a Question