0

I have a text file like below:

Execution ID, Unique number, Result 1, Result 2
1234567 , 1002 , Dron,  User suppressed due to Inactivity Rule
1234567 , 1002 , Dron,  User suppressed due to Wrong Email Address
2348976 , 1003 , Dron,  User suppressed due to Language Rule

I want the above text to be converted like below:

Execution ID: 1234567
Unique number: 1002
Result 1: Dron
Result 2: User suppressed due to Inactivity Rule, User suppressed due to Wrong Email Address

Execution ID: 2348976
Unique number: 1003
Result 1: Dron
Result 2: User suppressed due to Language Rule
4
  • Try this awk 'BEGIN{FS=", "}NR==1{split($0, h)}NR>1{print h[1] ": " $1; print h[2] ": " $2; print h[3] ": " $3; print h[4] ": " $4; print ""}' yourtextfile. Commented Dec 24, 2019 at 10:39
  • Or better awk 'BEGIN{FS=", "}NR==1{split($0, h)}NR>1{printf "%s: %s\n%s: %s\n%s: %s\n%s: %s\n\n",h[1],$1,h[2],$2,h[3],$3,h[4],$4}' yourtextfile Commented Dec 24, 2019 at 10:44
  • Did you already tried something? Please share with us! Commented Dec 24, 2019 at 11:20
  • Editing your question after recieving a correct answer is counter-productive there! Commented Dec 24, 2019 at 11:45

2 Answers 2

1

You can do it using awk like this:

awk 'BEGIN{FS=", "}NR==1{split($0, h)}NR>1{printf "%s: %s\n%s: %s\n%s: %s\n%s: %s\n\n",h[1],$1,h[2],$2,h[3],$3,h[4],$4}' textfile

And here is a proof of concept:

$ awk -V | head -1
GNU Awk 5.0.1, API: 3.0 (GNU MPFR 3.1.6-p2, GNU MP 6.1.2)

$ cat csv 
Execution ID, Unique number, Result 1, Result 2
1234567, 1002, Dron, User suppressed due to Inactivity Rule
2348976, 1002, Dron, User suppressed due to Language Rule

$ awk 'BEGIN{FS=", "}NR==1{split($0, h)}NR>1{printf "%s: %s\n%s: %s\n%s: %s\n%s: %s\n\n",h[1],$1,h[2],$2,h[3],$3,h[4],$4}' csv 
Execution ID: 1234567
Unique number: 1002
Result 1: Dron
Result 2: User suppressed due to Inactivity Rule

Execution ID: 2348976
Unique number: 1002
Result 1: Dron
Result 2: User suppressed due to Language Rule

$ 
Sign up to request clarification or add additional context in comments.

7 Comments

Your solution worked. Many Thanks. But I missed to add one use case when my Execution Id and Unique number are same then the Result 1 should be deduped and Result 2 values should be combined with comma separated. Can you please help to achieve this usecase as well. I have updated the question.
Update your question with examples of input and output desired. My solution is based on what you originally shared on the post.
Your updated question is a completely different thing and I can't help you on that.
Not a problem. Thanks for looking into this. Hence Upvoting your answer
Hardcoding the form (with fixed 4 fields) is a bad idea! You'd better built form from number of fields!
|
0

Could you please try following.

awk '
BEGIN{
  FS=SUBSEP=OFS=","
}
FNR==1{
  split($0,header,", ")
  next
}
{
  a[$1,$2,$3]=(a[$1,$2,$3]?a[$1,$2,$3]":":"")$4
}
END{
  for(i in a){
    val=i OFS a[i]
    num=split(val,array,",")
    for(o=1;o<=num;o++){
      print header[o],array[o]
    }
    delete array
  }
}
' Input_file


Explanation: Adding a detailed explanation for above code.

awk '                                                     ##Starting awk program from here.
BEGIN{                                                    ##Starting BEGIN section here.
  FS=SUBSEP=OFS=","                                       ##Setting FS, OFS and SUBSEP as comma here.
}                                                         ##Closing BEGIN BLOCK for this awk code.
FNR==1{                                                   ##Checking condition if this is first line then do following.
  split($0,header,",")                                    ##Splitting line into an array named header whose delimiter is comma here.
  next                                                    ##next will skip all further statements from here.
}                                                         ##Closing BLOCK for FNR==1 condition here.
{
  a[$1,$2,$3]=(a[$1,$2,$3]?a[$1,$2,$3]":":"")$4           ##Creating an array named a whose index is $1,$2,$3 and value is $4 with concatenating each line its own value in it.
}
END{                                                      ##Starting END block for this awk code here.
  for(i in a){                                            ##Traversing through array a all elements.
    val=i OFS a[i]                                        ##Creating a variable val and its value is variable i(index of array a) OFS and array a value here.
    num=split(val,array,",")                              ##Splitting variable val to array named array whose delimiter is comma here.
    for(o=1;o<=num;o++){                                  ##Starting a for loop from o=1 to till value of num.
      print header[o],array[o]                            ##Printing header array with index of o and array with index of o here.
    }
    delete array                                          ##Deleting array here, to avoid confusions, since each cycle new array will be created.
  }
}
'  Input_file                                             ##Mentioning Input_file name here.

1 Comment

I have updated my question to include one more scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.