Need to parse lines in a file into two words and assign the values to two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to parse lines in a file into two words and assign the values to two variables
# 1  
Old 06-28-2011
Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths.
Code:
* /vobs/fts/FTSUSM20_VOB  /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB 
* /vobs/fts/FTS20_VOB  /ccvobsslx01/projects/vobs/eml/FTS20_VOB 
* /vobs/pmv/PMS_VOB    /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB 
* /vobs/compub/TMNcompub  /ccvobsslx01/projects/vobs/cpm/_/TMNcompub 
* /vobs/PARSER.vob0    /ccvobsslx01/projects/vobs/eml/PARSER.vbs
* /vobs/SH5.vob0       /ccvobsslx01/projects/vobs/eml/_/SH5.vob0 
* /vobs/SH5.vob1       /ccvobsslx01/projects/vobs/eml/_/SH5.vob1

Here, I need to parse each line and assign VOB TAG as a separate variable and VOB path as a separate variable.
vobs/fts/FTSUSM20_VOB should be the first variable.
/ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB should be the second variable

Can the delimiter be a 'space' and / ?

After this, I will perform some operations with the VOBs..

Pls help me out if possible.

Last edited by Franklin52; 06-28-2011 at 07:37 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 06-28-2011
Code:
$ sed 's,^,var1=,g;s, ,; var2=,1' input_file
var1=/vobs/fts/FTSUSM20_VOB; var2=/ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB
var1=/vobs/fts/FTS20_VOB; var2=/ccvobsslx01/projects/vobs/eml/FTS20_VOB
var1=/vobs/pmv/PMS_VOB; var2=/ccvobsslx01/projects/vobs/cpm/_/PMS_VOB
var1=/vobs/compub/TMNcompub; var2=/ccvobsslx01/projects/vobs/cpm/_/TMNcompub
var1=/vobs/PARSER.vob0; var2=/ccvobsslx01/projects/vobs/eml/PARSER.vbs
var1=/vobs/SH5.vob0; var2=/ccvobsslx01/projects/vobs/eml/_/SH5.vob0
var1=/vobs/SH5.vob1; var2=/ccvobsslx01/projects/vobs/eml/_/SH5.vob1


Last edited by Franklin52; 06-28-2011 at 07:38 AM.. Reason: Code tags
# 3  
Old 06-28-2011
Code:
 
while read vob_tag vob_path
do
  #do your operation here
  echo $vob_tag
  echo $vob_path
done < inputfile

# 4  
Old 06-28-2011
@ Jay -> How do I remove spaces in Var2..
Code:
var1=/vobs/fts/FTSSRVSNMP_VOB; var2=/ccvobsslx01/projects/vobs/eml/FTSSRVSNMP_VOB 
var1=/vobs/fts/FTSUSM20_VOB; var2=/ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB 
var1=/vobs/fts/FTS20_VOB; var2= /ccvobsslx01/projects/vobs/eml/FTS20_VOB 
var1=/vobs/pmv/PMS_VOB; var2=   /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB 
var1=/vobs/compub/TMNcompub; var2=/ccvobsslx01/projects/vobs/cpm/_/TMNcompub 
var1=/vobs/PARSER.vob0; var2=   /ccvobsslx01/projects/vobs/eml/PARSER.vbs 
var1=/vobs/SH5.vob0; var2=      /ccvobsslx01/projects/vobs/eml/_/SH5.vob0 
var1=/vobs/SH5.vob1; var2=      /ccvobsslx01/projects/vobs/eml/_/SH5.vob1


Last edited by Franklin52; 06-28-2011 at 07:38 AM.. Reason: Please use code tags for code and data samples, thank you
# 5  
Old 06-28-2011
just removed the space in the code...
Code:
$ sed 's,^,var1=,g;s, ,;var2=,1' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign correct values to the multiple words?

The file1 contains mistakes and looks like 1 No one have never become rich by giving. Anne Dickens 2 No one is worthless in this globe who lightens the weights of other. Charles_Dickens file2 contains the correction of words and looks like rich poor have has never ever... (3 Replies)
Discussion started by: sammy777888
3 Replies

2. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

6. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

7. Shell Programming and Scripting

Assign values to variables of a file

Hi, I have a file like the following... CUST= DIR= NULIST= name=philps_123 How can i add values to each of these unassigned variables using a shell script? say for eg: i have values for CUST as onida, dir as /dir/onida, NULIST as /tmp/onida_files. How can i add these values to... (11 Replies)
Discussion started by: Tuxidow
11 Replies

8. Shell Programming and Scripting

Assign Values to variables from a text file

The text file has one single row and looks like this Q1 P1 2006 I have to pick up this values from a shell script into three different variables, say quarter, period and year from the above text file. Some one know's how to do this? I went through 'sed', dint really know how to... (3 Replies)
Discussion started by: sarsani
3 Replies

9. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

10. Programming

how do u assign the values to different variables when it is presneted in one line??

hey people.. i have a configuration file that looks like 7080 7988 net04.xxxxx.edu 20 where 20 is the number of threads in the thread pool initially. net04.xxxxx.edu is the hostname. and 7080 7988 are two ports. first one for client requests and second one for dns communication. now my... (2 Replies)
Discussion started by: SwetaShah
2 Replies
Login or Register to Ask a Question