Cut each column value and store in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut each column value and store in variable
# 1  
Old 08-28-2015
Cut each column value and store in variable

Hi Pals,

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

Code:
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:

Code:
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.
# 2  
Old 08-28-2015
Hello Khushbu,

Could you please try following and let me know if that helps you.
Code:
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.
Code:
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.
Code:
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

Last edited by RavinderSingh13; 08-28-2015 at 08:58 AM.. Reason: Added non one-liner form of solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-28-2015
What be the field separator (other than space!), or are we dealing with fixed width fields (assuming a typo in your sample).
# 4  
Old 08-28-2015
Hi Ravinder,

It Worked!! SmilieSmilie

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! :-D Thanks a lot lot!
# 5  
Old 08-28-2015
Unfortunately, you didn't tell us what the field separator is. Were it a <TAB> char, this might work:
Code:
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

This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-28-2015
Hello Khsuhbu,

Following may help you in same.
Code:
 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.
Code:
 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
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 08-28-2015
Hi ravinder,

This worked exactly as I needed! :-)
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 find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

How to store file name in a variable?

Hi All, Daily I am generating a file dfm_daily_file_ ex: dfm_daily_file_05072015 date will be changed daily. Once the file is FTP it is deleted. I have tried the below code to get the file name with any date and store it in a variable its not working. #!/bin/ksh ... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. Shell Programming and Scripting

I can't store value in the variable

!#bin/bash clear var= grep @gmail.com email.txt | wc -l echo $var echo $var exit 0 OUTPUT: 1000 _ _ Where _ represent space (no value or nothing) (4 Replies)
Discussion started by: Muhammad Rehan
4 Replies

4. Shell Programming and Scripting

How do I store stdout in a variable?

These seems ridiculously simple but I can't get it to work. Using korn shell and I want to pass in a flag to tell my echo statements to either write to the screen for debugging or a file if not. So I have something like: if ; then logout=&1 else logout='logfile.out' fi Then... (2 Replies)
Discussion started by: DJR
2 Replies

5. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

6. Shell Programming and Scripting

Store a column from an excel sheet (exported to txt) into a variable

I typically pull a bunch of data via SQL that lists a bunch of users and the server on which they want to access, as well as other attributes, in one row of an excel sheet and the number of rows is directly proportionate to the number of users. I'm trying to write a loop to read each line of the... (2 Replies)
Discussion started by: MaindotC
2 Replies

7. Shell Programming and Scripting

cut and store last value of a variable into other

Hi All, I am a newbie to unix.starting my career in unix.need 1 help from you all..pls help.. i am passing a file name "abc_delta" as argument to my script1.sh. if file name contains "_delta" at last then echo pass else fail.how to fix it. Note:file name will always contain "_delta" at... (10 Replies)
Discussion started by: pradeepcarya
10 Replies

8. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

9. Shell Programming and Scripting

remove column and store output to a variable

Hello guys I need to run a script to remove the last column of different comma separated files. The problem is that the number of columns of my files will be different and I won't know that number every time i run my script. Is there any command I can use to remove the last column without... (7 Replies)
Discussion started by: loperam
7 Replies

10. UNIX for Dummies Questions & Answers

How to Store command O/P to a variable...

Hi all.. I got a problem.. Its easy to redirect o/p to a file.. But Is it possible to redirect the O/P to a variable? For example: I've a command in my script: string1=cut -d ':' -f2 file.txt When I do: echo $string1 The value is empty... Pls suggest me how to store the value... (7 Replies)
Discussion started by: smartbuddy
7 Replies
Login or Register to Ask a Question