Parsing fields into variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing fields into variables
# 1  
Old 02-12-2016
Parsing fields into variables

A record contains 50 fields separated by "~". I need to assign each of these fields to different variables. Following is the shell script approach I tried.

Code:
RECORD="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5~...........~FIELD50"
VAR1=$(echo ${RECORD} | cut -d"~" -f 1)
VAR2=$(echo ${RECORD} | cut -d"~" -f 2)
VAR3=$(echo ${RECORD} | cut -d"~" -f 3)
.
.
.
.
.
VAR50=$(echo ${RECORD} | cut -d"~" -f 50)

Please share alternate approach to save these variables that might reduce the lines of code.

I'm using these variables further in the script for more operations around them.
# 2  
Old 02-12-2016
Hello krishmaths,

As you haven't shown us the complete requirement here so little difficult to say how you wanted to use these 50 variables. But wanted to highlight here for these kind of requirements only we have array concept, here is an example for same where you could get an array with all the elements in the lines and if you wanted to use any specific position of that array's element you could do so.
Code:
awk '{num=split($0, array,"~");for(i=1;i<=num;i++){print A[i]}}' Input_file

Also if above doesn't meet your requirements then please let us know more about requirement with sample inputs and sample expected output too.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-12-2016
I wanted to use specific variable names for better readability. For example, the actual field name is more readable than an array name with index. Hence did not go for array.

Another approach I tried is using the
Code:
read

command.

Code:
echo ${RECORD} | read VAR1 VAR2 VAR3

It works with space delimiter. I'm not sure how to handle this for other delimiters.
# 4  
Old 02-12-2016
Hello krishmaths,

Could you please try and let me know if this helps you. Let's say following is the Input_file.
Code:
cat  Input_file
A~B CDR~rsbkjdewdj @!!!~deiiugewiuewn

Then following is the code.
Code:
while IFS="~" read -r name dept cool; do
     echo $dept
done < "Input_file"

Output will be as follows then. Similarly we could take all variable values from it.
Code:
B CDR

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 02-12-2016
Thanks for your inputs. I tried the following and it worked. I needed to read from variable and not from a file.

Code:
RECORD="F1~F2~F3"
echo ${RECORD} | IFS="~" read VAR1 VAR2 VAR3
echo $VAR1 $VAR2 $VAR3

Output:
Code:
F1 F2 F3

# 6  
Old 02-12-2016
Got a recent bash providing "here strings"? Try
Code:
RECORD="F1~F2~F3"
IFS="~" read VAR1 VAR2 VAR3 <<< $RECORD
echo $VAR1 $VAR2 $VAR3
F1 F2 F3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a file and setting to variables.

greetings all, I have a task right now that is somewhat stumping me, and I am not sure what the best approach is to take it. I have a text file that will contain something similar to the following: first1, other1 first2, other2 first3, other3 first4, other4 I have to generate an... (14 Replies)
Discussion started by: jeffs42885
14 Replies

2. Shell Programming and Scripting

Parsing fields from class list files to use output with newusers command

Hello I am trying to develop a shell script that takes a text file such as this... E-mail@ Soc.Sec.No. *--------Name-----------* Class *School.Curriculum.Major.* Campus.Phone JCC2380 XXX-XX-XXXX CAREY, JULIE C JR-II BISS CPSC BS INFO TECH 412/779-9445 JAC1936 XXX-XX-XXXX... (7 Replies)
Discussion started by: crimputt
7 Replies

3. UNIX for Dummies Questions & Answers

Parsing alphanumeric variables

Hi All, I have files with a column which has values and ranges, for example colA colB ERD1 3456 ERD2 ERD3 4456 I want to have the following output colA colB colC ERD1 3456 3456 ERD2 526887 526890 ERD3 4456 4456 Being a newbie to... (2 Replies)
Discussion started by: alpesh
2 Replies

4. UNIX for Dummies Questions & Answers

Set variables from fields in fields

Hi, This is my first post here and I am a newbie. :) I have a file that looks like this : Introduction:Intro_123.html Product definition:Prod_def.html System Setup:SSetup-64bit.html Setting up user accounts:Set_user_acc.html I tried to create a script that would output "The filename... (3 Replies)
Discussion started by: Joq
3 Replies

5. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies

6. Shell Programming and Scripting

parsing text three fields at a time

I'm programming in csh and I have a text file with hundreds of entries seperated only by spaces. I want to access three fields at a time (as each data set has three components) so that I can send these values to a different routine as three variables until every trio of values in the text file has... (3 Replies)
Discussion started by: weak_code-fu
3 Replies

7. Shell Programming and Scripting

Help parsing logs maybe with menu and variables?

I would like to parse through some logs looking for things like exception or failed (grep -i failed). Ideal would be if it were in a menu format so someone without unix ability could just choose option 1 2 or 3 etc. If I could pass the hostname to a variable also that would be awesome, so someone... (5 Replies)
Discussion started by: taekwondo
5 Replies

8. Shell Programming and Scripting

Perl: parsing variables

I have the following script: #!/usr/bin/perl -w @files = <*.csv>; foreach $file (@files) { open(FH, $file); my @dt = split(/_|.csv/, $file); while (<FH>) { chomp; print $dt . $dt . ",$_\n"; } close(FH); } This script reads in all csv files in the current directory... (2 Replies)
Discussion started by: figaro
2 Replies

9. Shell Programming and Scripting

parsing data file picking out certain fields

I have a file that is large and is broken up by groups of data. I want to take certain fields and display them different to make it easier to read. Given input file below: 2008 fl01 LAC 2589 polk doal xx 2008q1 mx sect 25698541 Sales 08 Dept group lead1 ... (8 Replies)
Discussion started by: timj123
8 Replies

10. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies
Login or Register to Ask a Question