sort piping to awk array - help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort piping to awk array - help please
# 1  
Old 11-08-2010
sort piping to awk array - help please

Hi I'm just learning programming and need some help.

I've taken a data file which has a list of numbers eg:
3
5
32
533
13
2

And I've used sort -n and to sort and then piped it to awk to arrange into an array.

Code:
#!/bin/sh

sort -n data.txt |

awk '
{
  array[$2]=$1
}
END{
  for(i in array) {
    print array[i],i
     }
}
'

I've gotten this far but knowing a little C I know that I'm not loading the array correctly as there should be some sort of loop. But I can't find any good tutorials to show me how to do what I want to do.

Any help would be much appreciated!
# 2  
Old 11-08-2010
Hi.

Where does $2 in array[$2]=$1 come from?

As per the input you've shown, there is no "$2".

What are you hoping to achieve? The order in which elements in a for( i in array ) construct are returned is "arbitrary" (thus, most likely messing up your nicely sorted input).
# 3  
Old 11-08-2010
Ah scott, thanks for that, I removed the $2, I think I added it in when I was testing out some examples I found on the net.

I don't quite understand the second bit your wrote thou. I am now getting the data.txt printed out but not in sort order. So I know what your saying but don't quite know what your getting at. I should also have 20 numbers some of them duplicates but I'm only getting about 14. I think the duplicates are being rolled into one.

Thanks for the speedy reply!
# 4  
Old 11-08-2010
Hi.

I was asking what your intended output should be.

If your sole input is
Code:
3
5
32
533
13
2

and you want it sorted numerically, then sort -n will do that without awk.

Code:
$ sort -n data.txt
2
3
5
13
32
533

so I'm not sure where awk fits into your problem.

edit: I see you edited your post... something about 20 numbers and you're only getting 14...

Code:
sort -n file1 | awk '
  { array[NR] = $0 }
  END { for( i in array )
          print array[i], i
  }
'

sort -n file1 | awk '
  { array[++C] = $0 }
  END { for( i = 1; i <= C; i++ )
          print array[i], i
  }
'


Last edited by Scott; 11-08-2010 at 08:53 PM..
This User Gave Thanks to Scott For This Post:
# 5  
Old 11-08-2010
Thanks for that scott, exactly what I needed to get my head around. Once I (you) had it in the array an understood how it worked in awk the rest was easy, so thank you for that!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Array length: ls and sort

Hi there, I'm listing files and sorting them. When I try to get length of array variable in which these files are stored I get 1 as value. That's weird. files_info="$(find $input_dir -name "*_CHR$i.info" | sort )" printf ${#files_info}"\n" #print length #--loop through... (6 Replies)
Discussion started by: genome
6 Replies

2. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

3. Emergency UNIX and Linux Support

How to take awk result out (piping to other program)?

Hi! all here is my code which is working fine no errors but I want to know how to take result and input to other program awk 'FNR==1{i++}{LC=NR} {for(k=1; k<=NF; k++) A=$k} END{for (i=1;i<=LC;i++) { for(j=1;j<=LC;j++) if(A=='$UID' && A>='$MX'+A &&... (7 Replies)
Discussion started by: Akshay Hegde
7 Replies

4. UNIX for Dummies Questions & Answers

Weird: unexpected result after piping a sort

Hello, And when you think you know the basics of something, UNIX in this case, something like what I will describe below comes along.... On a Linux system, a "typical" directory with some files. Say 20. I do: > ls | sort > mylisting Now when I: > vi mylisting There is mylisting... (13 Replies)
Discussion started by: stavros
13 Replies

5. Shell Programming and Scripting

awk array sort

I have a file as below Input File: 5/11/2012, dir1, 134 5/11/2012, dir2, 2341 5/11/2012, dir3, 2134 5/11/2012, dir4, 2334 5/12/2012, dir1, 234 5/12/2012, dir2, 2341 5/12/2012, dir3, 2334 5/13/2012, dir1, 234 5/13/2012, dir2, 2341 5/13/2012, dir3, 2334 5/13/2012, dir4, 21134 Now... (6 Replies)
Discussion started by: chakrapani
6 Replies

6. Shell Programming and Scripting

piping from grep to awk without intermediate files

I am trying to extract the file names alone, for example "TVLI_STATS_NRT_XLSTWS03_20120215_132629.csv", from below output which was given by the grep. sam:/data/log: grep "C10_Subscribe.000|subscribe|newfile|" PDEWG511_TVLI_JOB_STATS.ksh.201202* Output: ... (6 Replies)
Discussion started by: siteregsam
6 Replies

7. Shell Programming and Scripting

Piping Unix Variable Array values into AWK

#ksh Here is my code: ERRORLIST="43032 12001 12002 12003 12004 34019 49015 49016 49017 49018 49024 49025 49026 58004 72003 12005 12006 12007 12008 12011 12012 16024 16023" for ERROR in ${ERRORLIST} do awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: '"${ERROR}"'/{print... (3 Replies)
Discussion started by: k1ko
3 Replies

8. Shell Programming and Scripting

Piping tail to awk to parse a log file

Hello all, I've got what I'm pretty sure is a simple problem, but I just can't seem to work past it. I'm trying to use awk to pretty up a log file, and calculate a percentage. The log file looks like this: # tail strtovrUsage 20090531-18:15:45 RSreq - 24, RSsuc - 24, RSrun - 78, RSerr -... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

9. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

10. Shell Programming and Scripting

How To Sort Array of Hashes ??

Some one plz help me how to sort an array of hashes ..... for e.g i have an array as @AoH = ( { ques => 10, marks => 32, }, { ques => 32, marks => 22, }, { ques => 2, marks => 41, }, ); now i want to sort this array with increasing value of "ques" ..... plz... (3 Replies)
Discussion started by: coolguyshail
3 Replies
Login or Register to Ask a Question