LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-22-2017, 05:11 PM   #1
beca123456
LQ Newbie
 
Registered: Apr 2016
Posts: 10

Rep: Reputation: Disabled
awk: align values horizontally to a template record header


Hi,

my input looks like this:
Code:
PASS|A(1/2);B(3/4);C(5/6)
PASS|A(7/8);C(9/10)
FAIL|B(11/12)
I am trying to isolate the individual ";"-separated strings from $2 in identical fields based on the first letter.
example from record 1 of input above:
letter1=A
letter1_val1=1
letter1_val2=2

letter2=B
letter2_val1=3
letter2_val2=4

letter3=C
letter3_val1=5
letter3_val2=6

Output would be:
Code:
Test|A_val1|A_val2|B_val1|B_val2|C_val1|C_val2
PASS|1|2|3|4|5|6
PASS|7|8|-|-|9|10
FAIL|-|-|11|12|-|-

Note that I know in advance how the final header (here in red) will look like (i.e. here I know I have 3 different letters in $2).

So far I managed to split the different letters and their values but I am struggling to order them.

Code:
awk '
BEGIN{
   FS=OFS="|"
   
   # The output header is known in advance
   header =  "Test|A_val1|A_val2|B_val1|B_val2|C_val1|C_val2"
   a=split(header,b,"|")

   # val1 record indexes   
   for(i=2; i<=a; i+=2){
      letter_val1 = b[i]
      gsub(/_val1/,"",letter_val1)
      c[letter_val1]=i
   }

   # val2 record indexes
   for(j=3; j<= a; j+=2){
      letter_val2 = b[j]
      gsub(/_val2/,"",letter_val2)
      d[letter_val2]=j
   }

   print header
}
{
   # isolate ";"-separated string from $2 and their respective 'letter', 'val1' and 'val2'
   e=split($2,f,";")
   for(k=1; k<=e; k++){
      split(f[k],g,"[\(/\)]")

      letter=g[1]
      val1=g[2]
      val2=g[3]

   # match letter and val1 with proper record number
      if(letter in c){
         $(c[letter])=val1
      }

   # match letter and val2 with proper record number
      if(letter in d){
         $(d[letter])=val2
      }

   for(l=1; l<=NF; l++){
      if($l ~ /^$/){
         $l="-"
      }
   }

   print $0

} input > output
The code above produces this output:
Code:
Test|A_val1|A_val2|B_val1|B_val2|C_val1|C_val2
PASS|1|2|3|4|5|6
PASS|7|8|-|-|9|10
FAIL|B(11/12)|-|11|12
EDIT: Find a way to fill the blanks, but the problem now is when $2 does not begin by letter 'A' (see record 3 above).

Last edited by beca123456; 03-22-2017 at 05:55 PM.
 
Old 03-22-2017, 09:05 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,148

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
The problem is you not validating the data. Some simple diagnostics will show your error - say print "e" and the contents of "f[]" for each of those records.
 
  


Reply

Tags
awk, join


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Select values from multiple rows comprising a record devUnix Linux - General 14 05-31-2016 01:56 AM
awk: print sum of array values and remaining record beca123456 Linux - Newbie 6 05-07-2016 08:14 PM
[SOLVED] IP header record route option narender14 Linux - Networking 2 08-14-2013 06:07 PM
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 07:00 PM
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 04:20 PM

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

All times are GMT -5. The time now is 08:52 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration