LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-04-2012, 10:02 PM   #1
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Rep: Reputation: Disabled
Problem ternary (?:) in awk


Hi Linux Gurus !

My input looks like:
Code:
first_A|last_B|bio|score_100
first_A|last_B|math|score_50
first_C|last_D|bio|score_70
first_C|last_D|phys|score_35
first_C|last_D|math|score_55
I would need to append on a same line all the information related to the same first and second fields:
Code:
first_A|last_B|bio|score_100|math|score_50
first_C|last_D|bio|score_70|phys|score_35|math|score_55
For that I tried to use the ternary ( ?: ) as follow:
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 get all the intermediate steps until I reach the output I want:
Code:
first_A|last_B|bio|score_100
first_A|last_B|bio|score_100|math|score_50
first_C|last_D|bio|score_70
first_C|last_D|bio|score_70|phys|score_35
first_C|last_D|bio|score_70|phys|score_35|math|score_55
I tried to empty the variable first but it cannot save the previous field and I get the same as input:
Code:
BEGIN{FS=OFS="|"}

{
    f1 = ""  
    f1 = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
    $0 = f1
    print $0
}
Does someone see what I am doing wrong?

Last edited by Trd300; 10-04-2012 at 11:16 PM.
 
Old 10-05-2012, 02:44 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You need an END part to print the content of the array.

Instead of:
Code:
BEGIN{FS=OFS="|"}

{
    array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
    print array[$1$2]
}
try
Code:
BEGIN { FS=OFS="|" }
{ 
  array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
}
END { for (var in array) print array[var] }
You cannot predict the order in which the array is printed, you might need to sort the output (can probably done with awk, but piping it to sort might be easier):

Code:
#!/bin/bash

awk 'BEGIN { FS=OFS="|" }
{
  array[$1$2] = (array[$1$2] ? array[$1$2] : $1 FS $2) FS $3 FS $4
}
END { for (var in array) print array[var] }
' infile | \
sort
 
1 members found this post helpful.
Old 10-06-2012, 07:41 PM   #3
Trd300
Member
 
Registered: Feb 2012
Posts: 89

Original Poster
Rep: Reputation: Disabled
Thanks druuna !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Control flow problem using conditional ternary operator in C Blackened Justice Programming 6 06-25-2012 11:49 PM
[SOLVED] Python equivalent of C ternary operator. pr_deltoid Programming 6 06-12-2010 04:31 PM
Ternary operator for strings in BASH tifkat Programming 3 10-13-2009 07:40 PM
if else vs ternary operator hottdogg Programming 2 01-16-2007 11:21 AM
ternary search nimishabhatia Programming 1 02-11-2005 10:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration