Cut each column value and store in variable

Hi Pals,

I have a file which contains a set of similar lines, as follows:

Remedy Support                      Action Triggered by Incident Modification of type: ARA       username2             ########## ARA|INC0000178532  INC0000178532      0000000019879      000000000038372
Remedy Configuration & Maintenance  Action Triggered by Incident Modification of type: ARA       username1               ########## ARA|INC0000085423  INC0000085423      0000000000073      000000000054

I want to cut each column of each line and store in a variable which will be used to pass to another script with these variables as parameters. Something like follows:

var1 = Remedy Support                    
var2 = Action Triggered by Incident Modification of type: ARA       
var3 = username2
var4 = ########## 
var5 = ARA|INC0000178532  
var6 = INC0000085423
var7 = 0000000019879
var8 = 000000000038372
  

Then , pass these variables to another script as parameters.

Likewise, i want to do this fo each line in file; this will need loop i suppose. (How to achieve this?)

Can you please help how do i achieve this cut, storing in variable and passing these as parameters to another script?

Thanks.

Hello Khushbu,

Could you please try following and let me know if that helps you.

awk -F"Action Triggered by Incident Modification of type: ARA" '{S[++o]="var1=" $1 ORS "var2=" FS;$1="";gsub(/^[[:space:]]+/,X,$0);split($0,D," ");{print S[o] ORS "var3=" D[1] ORS "var4=" D[2] ORS "var5=" D[3] ORS "var6=" D[4] ORS "var7=" D[5] ORS "var8=" D[6]}}'  Input_file
 

Output will be as follows.

var1=Remedy Support
var2=Action Triggered by Incident Modification of type: ARA
var3=username2
var4=##########
var5=ARA|INC0000178532
var6=INC0000178532
var7=0000000019879
var8=000000000038372
var1=Remedy Configuration & Maintenance
var2=Action Triggered by Incident Modification of type: ARA
var3=username1
var4=##########
var5=ARA|INC0000085423
var6=INC0000085423
var7=0000000000073
var8=000000000054
 

EDIT: Following is the non one-liner solution for same.

awk -F"Action Triggered by Incident Modification of type: ARA" '{
                                                                        S[++o]="var1=" $1 ORS "var2=" FS;
                                                                        $1="";
                                                                        gsub(/^[[:space:]]+/,X,$0);
                                                                        split($0,D," ");
                                                                {
                                                                        print S[o] ORS "var3=" D[1] ORS "var4=" D[2] ORS "var5=" D[3] ORS "var6=" D[4] ORS "var7=" D[5] ORS "var8=" D[6]
                                                                }
                                                                }
                                                               ' Input_file
 

Thanks,
R. Singh

1 Like

What be the field separator (other than space!), or are we dealing with fixed width fields (assuming a typo in your sample).

Hi Ravinder,

It Worked!! :b::slight_smile:

Thank You so much!

Actually, the data that we will get would be run time data. And the first two columns namely var1 and var2 are highly unprdedicatble since these will be combination of various words. So I tried all possible combinations i knew but couldnt achieve this. Hope, this code works for all kinds ofvar1 and var2.

Now since, we have acheived this, I wanna know how can i use these variables to pass as parameters to another script? I dont know such advanced UNIX.. so can you please help me out..

---------- Post updated at 07:51 AM ---------- Previous update was at 07:37 AM ----------

One more requirement:
As in the code above, i want the varaibles to be reused in another script, so i cant get them printed on command prompt. I would like them to store somewhere where i can reuse them. So how can the above code be modified?

The code runs perfectly for as many as lines i enter! :smiley: Thanks a lot lot!

Unfortunately, you didn't tell us what the field separator is. Were it a <TAB> char, this might work:

while IFS=$'\t' read var1 var2 var3 var4 var5 var6 var7 REST; do echo $var1, $var2, $var3, $var4, $var5, $var6, $var7; done < file
Remedy Support, Action Triggered by Incident Modification of type: ARA, username2, ########## ARA|INC0000178532, INC0000178532, 0000000019879, 000000000038372
Remedy Configuration & Maintenance, Action Triggered by Incident Modification of type: ARA, username1, ########## ARA|INC0000085423, INC0000085423, 0000000000073, 000000000054
1 Like

Hello Khsuhbu,

Following may help you in same.

 awk -F"Action Triggered by Incident Modification of type: ARA" '{S[++o]="var1=" $1 ORS "var2=" FS;$1="";gsub(/^[[:space:]]+/,X,$0);split($0,D," ");{print S[o] ORS "var3=" D[1] ORS "var4=" D[2] ORS "var5=" D[3] ORS "var6=" D[4] ORS "var7=" D[5] ORS "var8=" D[6]}}' Input_file > Output_file
 

Now we can see output will be as follow in file.

 cat Output_file
var1=Remedy Support
var2=Action Triggered by Incident Modification of type: ARA
var3=username2
var4=##########
var5=ARA|INC0000178532
var6=INC0000178532
var7=0000000019879
var8=000000000038372
var1=Remedy Configuration & Maintenance
var2=Action Triggered by Incident Modification of type: ARA
var3=username1
var4=##########
var5=ARA|INC0000085423
var6=INC0000085423
var7=0000000000073
var8=000000000054
 

Now if you want to call these variables into a new script as variable pass, you can use it but it will be little tricky one as per your requirement we need to always print variables from var1 to var8 only so there will be duplicate entries in output file, if you can open your requirement little broader it will be better for us to understand and help you, hope this helps.

Thanks,
R. Singh

1 Like

Hi ravinder,

This worked exactly as I needed! :slight_smile: