Read 1-line file and separate into multiple variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read 1-line file and separate into multiple variables
# 1  
Old 05-18-2010
Read 1-line file and separate into multiple variables

I have one line files with 17 records separated by a semi-colon. I need to create a variable from each record, which I can do via a separate awk for each one, but I know there has to be a better way. Along with pulling out the variable, I need to convert some url coding like a + to a space, etc.
Code:
#!/bin/sh
SUBJECT=`nawk 'BEGIN {FS=";"}{print $1;}' 100518-213`
NAME=`nawk 'BEGIN {FS=";"}{print $2;}' 100518-213`
EMAIL=`nawk 'BEGIN {FS=";"}{print $3;}' 100518-213`
...
PORT=`nawk 'BEGIN {FS=";"}{print $17;}' 100518-213`

Now the last 6 records can have multiple entries for the single record and I need to name each one differently. For example, my 17th records is called PORT and it can have multple entries and I need a PORT1 PORT2 PORT3 and so on.

Here is a sample single line record:
Code:
100518-213;John+Smith;jsmith@gmail.com;
212-555-1212;js1234;OTHER;Universal+Exports;Normal;I+need+this+
request+completed+as+soon+as+possible+please.;Our+
department+needs+these+added+for+access+to+new+
servers;Please+update+our+group+when+completed.;
All 13-State 9-State;Add Add Delete;Router+Access Mail Other;
;10.1.1.1%2F24 192.168.1.1 10.2.1.1%2F22;23 25 80

# 2  
Old 05-19-2010
If we assume records separated by semicolon, 1 to 17 fields should be something like:

Code:
f1	100518-213;
f2	John+Smith;
f3	jsmith@gmail.com;
f4	212-555-1212;
f5	js1234;
f6	OTHER;
f7	Universal+Exports;
f8	Normal;
f9	I+need+this+request+completed+as+soon+as+possible+please.;
f10	Our+department+needs+these+added+for+access+to+new+servers;
f11	Please+update+our+group+when+completed.;
f12	All 13-State 9-State;
f13	Add Add Delete;
f14	Router+Access Mail Other;
f15	;
f16	10.1.1.1%2F24 192.168.1.1 10.2.1.1%2F22;
f17	23 25 80


Some questions:

how the multiple records (in the last 6 as you said) are recognized? separated by space always?

field 15 is null. is that a typo or fields can be null?

what is the final purpose to those variable? how do you going to use them?
perhaps your purpose can be solved in awk itself. (until it is really necessary to go back to the shell.)
# 3  
Old 05-19-2010
Here are the answers to your questions:

how the multiple records (in the last 6 as you said) are recognized? separated by space always? Yes, always separated by a space.

field 15 is null. is that a typo or fields can be null? Yes, fields can be null.

what is the final purpose to those variable? how do you going to use them? The variables will be used to fill in a template.

perhaps your purpose can be solved in awk itself. I am open to any solution.
# 4  
Old 05-20-2010
Ok, Don't have idea about template thing. but regarding your question,

you can do something like:

Code:
#!/bin/ksh
eval $(awk 'BEGIN { FS = ";"}
 
function _p (field,cnt)
{
 split(field,arr," ");
 for (n in arr)
 print "var_"cnt"_"n"="arr[n]
}
{ for(i=1;i<=11;i++){ print "var_"i"="$i }
  for(i=12;i<=17;i++){ if ($i != "") {_p($i,i)} else { print "var"i"="$i} }
}' filename) 

## shell part begins


echo $var_1
echo $var_2
echo $var_3
echo $var_4
echo $var_5
echo $var_6
echo $var_7
echo $var_8
echo $var_9
echo $var_10
echo $var_11
echo $var_12_2
echo $var_12_3
echo $var_12_1
echo $var_13_2
echo $var_13_3
echo $var_13_1
echo $var_14_2
echo $var_14_3
echo $var_14_1
echo $var15
echo $var_16_2
echo $var_16_3
echo $var_16_1
echo $var_17_2
echo $var_17_3
echo $var_17_1

Here, filename is the name of the file which contains that one line with 17 records separated by semicolon.

This will create the shell variables in the following manner:

Code:
for 1st field var_1
for 2nd field var_2
.....
so on


for multi field, the rule would be:

Code:
for 17 field, 1st value, var_17_1 
for 17 field, 2nd value, var_17_2
...
so on

# 5  
Old 05-20-2010
Thank you! I'll give it a try as it looks like it will work for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

2. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

3. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

4. Shell Programming and Scripting

Read each line and saving the line in separate files

Hi Experts, I am having a requirement like this; Input file EIM_ACCT.ifb|1001|1005 EIM_ADDR.ifb|1002|1004 EIM_ABD.ifb|1009|1007 I want to read each line of this file and pass each line,one at a time,as an argument to another script. eg; 1.read first line->store it to a file->call... (2 Replies)
Discussion started by: ashishpanchal85
2 Replies

5. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

6. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (1 Reply)
Discussion started by: erlanq
1 Replies

7. Shell Programming and Scripting

read one line file and separate into multiple lines

I have one long line text with semicolon used as separator between values in that line. Now, I want to separate the line into multiple line right after every 29th field. example input line: ... (2 Replies)
Discussion started by: erlanq
2 Replies

8. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

9. Shell Programming and Scripting

How to separate file to multiple line.

Hi Unix gurus, I am facing a problme with file. I need separate my file into multiple line. eg. Soure file: PRT07, aaa, bbb, 46, PRT06, ccc, ddd, 57, PRT05, eee,fff,aa, target file: PRT07, aaa, bbb, 46 PRT06, ccc, ddd, 57 PRT05, eee,fff,aa :wall: thanks in advance (4 Replies)
Discussion started by: ken002
4 Replies

10. Shell Programming and Scripting

Bash script to read a hostname and separate into variables

Hi All, I'm trying to concoct a bash script to use with a Puppet Implementation that will accept a hostname and break it down into variables. For example, my hostnames look like this --> machinename-group-building.example.com I'm looking for a way in the script to read until the first... (4 Replies)
Discussion started by: glarizza
4 Replies
Login or Register to Ask a Question