Sorting arrays horizontally without END section, awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting arrays horizontally without END section, awk
# 1  
Old 10-02-2012
Sorting arrays horizontally without END section, awk

input:
Code:
ref001, Europe, Belgium, 1001
ref001, Europe, Spain, 203
ref001, Europe, Germany, 457
ref002, America, Canada, 234
ref002, America, US, 87
ref002, America, Alaska, 652

Without using an END section, I need to write all the info related to the same ref number ($1)and continent ($2) on the same line, then to sort horizontally by descending order, the name of the country ($3) with its related number ($4), in order to get:

Code:
ref001, Europe, Spain, 203, Germany, 457, Belgium, 1001
ref002, America, US, 87, Canada, 234, Alaska, 652

I am stuck at the first step:
Code:
BEGIN{FS=OFS=","}

{    array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
    
     print array[$1$2]

}

but I obtain lines with $3 and $4 that append at the end at every step...

Last edited by lucasvs; 10-03-2012 at 07:30 AM..
# 2  
Old 10-02-2012
Why the constraint of not using the END pattern?
# 3  
Old 10-02-2012
It is part of a bigger script with several redirections, just to keep it simple
# 4  
Old 10-02-2012
Without an end section it will be difficult to terminate the last line with <lf>.
Code:
sort -k1,2 -k4n inputfile|
awk     '{if ($1!=tmp) {if (NR>1) print ""; printf "%s %s", $1, $2; tmp=$1} else printf ","}
     {printf " %s %s", $3, $4}
    '
ref001, Europe, Spain, 203, Germany, 457, Belgium, 1001
ref002, America, US, 87, Canada, 234, Alaska, 652$

# 5  
Old 10-02-2012
Quote:
Originally Posted by elixir_sinari
Why the constraint of not using the END pattern?
Quote:
Originally Posted by lucasvs
It is part of a bigger script with several redirections, just to keep it simple
Since you are asking for help with basic AWK tasks, you may not be competent to judge which approach is the simpler one. You may think that avoiding END is simpler, for whatever reason, when it may not be. This isn't the first thread in which you've recently requested an AWK solution that avoids END, and in each case the constraint complicates the code. This is an odd way to "keep it simple."

Perhaps if you shared the entire script we could confirm that your solution is actually the simplest, if not simplify it further.

In case it's relevant and you are not aware, an awk script can contain multiple END patterns, not just one. So if you are composing a solution from multiple source files, that's not a problem. END patterns will be executed in the order in which they appear.

Regards,
Alister
# 6  
Old 10-02-2012
alister: this task will be part of a function as I have to repeat it multiple times. Can a function refer to rules written in an END block?

Rudic: Thanks for your help. I understand the approach you are using. However the sort command doesn't work for me inside the script, so I tried asort function instead.
Code:
BEGIN{FS=OFS="|"}

{
    array = a[$1$2$3]
    n = asort(array, descending)
    for(u in array){
        print array[u] FS $4
    }
}

in order to get:
Code:
ref001, Europe, Spain, 203
ref001, Europe, Germany, 457
ref001, Europe, Belgium, 1001
ref002, America, US, 87
ref002, America, Canada, 234
ref002, America, Alaska, 652

but it returns an error message telling me the first argument is not an array. Is it doable with multiple fields at once?

Once I will manage to do that maybe I could do:
{
array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4

print array[$1$2]
}
but it will probably return:
Code:
ref001, Europe, Spain, 203
ref001, Europe, Spain, 203, Germany, 457
ref001, Europe, Spain, 203, Germany, 457, Belgium, 1001
ref002, America, US, 87
ref002, America, US, 87, Canada, 234
ref002, America, US, 87, Canada, 234, Alaska, 652

Then keep only the last occurrence of the line with the same "$1 FS $2", by sing:
Code:
{
    max = array[$1$2]++
    if(array[$1$2] = max){
        print $0
}

Or doing all these steps in reverse order and use horizontal sorting...

Last edited by jim mcnamara; 10-03-2012 at 12:17 AM..
# 7  
Old 10-03-2012
If I get your logics right, you will need to put the asort call into the END section which you want to avoid.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse section of csv into array

In the awk below I am trying to parse the Sample Name below the section. The values that are extracted are read into array s(each value in a row seperated by a space) which will be used later in a bash script. The awk does execute but no values are printed. I am also not sure how to print in a row... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Beginners Questions & Answers

Sorting blocks by a section of the identifier

I have a file that should be sorted by a string (shown in red in my example below) in the identifier. The RS is ^@M0, something like this: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCAGAAGCAGCAT... (16 Replies)
Discussion started by: Xterra
16 Replies

3. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

4. Shell Programming and Scripting

Warning while sorting : A newline character was added to the end of file

Hi, I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command, sort -t ',' +1... (5 Replies)
Discussion started by: DHeisenberg
5 Replies

5. Shell Programming and Scripting

using awk to get specific section of lines in logs

i have a log file that has the date and time that looks like this: Wed Jun 28 15:46:21 2012 test failed tailed passed passed not error panic what we want to focus on is the first 5 columns because they contain the date and time. the date and time can be anywhere on the line. in this... (6 Replies)
Discussion started by: SkySmart
6 Replies

6. Shell Programming and Scripting

Sort each row (horizontally) in AWK or any

Hello, How to sort each row in a document with numerical values and with more than one row. Example Input data (file1.txt): 4 6 8 1 7 2 12 9 6 10 6 1 14 5 7 and I want the the output to look like this(file2.txt): 1 4 6 7 8 2 6 9 10 12 1 5 6 7 14 I've tried sort -n file1.txt >... (12 Replies)
Discussion started by: joseamck
12 Replies

7. Shell Programming and Scripting

how to get one particular section (using awk)?

Hey, I have a problem about how to get one section of a file? I'm new to shell, but by reading some tutorial, I think I can use awk to do this. my input file: >ref|ZP_04937576.1| ECRINAEDPKTFMPSPGKVKHFHAPGGNGVRVDSHLYSGYSVPPNYDSLVGKVITYGAD DEALARMRNALDELIVDGIKTNTELHKDLVRDAAFCKGGVNIHYLE... (11 Replies)
Discussion started by: ritacc
11 Replies

8. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

9. Shell Programming and Scripting

Sorting rules on a text section

Hi all My text file looks like this: start doc ... (certain number of records) REC3|Emma|info| REC3|Lukas|info| REC3|Arthur|info| ... (certain number of records) end doc start doc ... (certain number of records)... (4 Replies)
Discussion started by: Indalecio
4 Replies

10. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies
Login or Register to Ask a Question