Cut a Variable into sub variables based on a delimiter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cut a Variable into sub variables based on a delimiter
# 1  
Old 05-04-2005
Cut a Variable into sub variables based on a delimiter

Hello All,
I am novice on Shell Scripting. Any help on this is highly appreciated.

I have a variable

$VARIABLE="$some1|$some2|$some3"

I need sub variables $SUBVAR1,$SUBVAR2,$SUBVAR3 which must be equal to $some1 , $some2 and $some3 respectively.

It works fine with

$SUBVAR1 = `echo $VARIABLE | AWK -F"|" '{print $1}'`
$SUBVAR2 = `echo $VARIABLE | AWK -F"|" '{print $2}'`
$SUBVAR3 = `echo $VARIABLE | AWK -F"|" '{print $3}'`

I needed this in a loop.
But I can't give print$i in the AWK.

In future the $VARIABLE may be expanded to $some4|$some5 and in that case I must have two new sub variables $SUBVAR4 and $SUBVAR5.


Any help on this is appreciated.


Thanks
jingi1234
# 2  
Old 05-04-2005
shell != perl

Code:
#!/bin/ksh

VARIABLE="some1|some2|some3"

echo "${VARIABLE}" | while IFS='|' read SUBVAR1  SUBVAR2 SUBVAR3 junk
do
    echo"SUBVAR1->[${SUBVAR1}] SUBVAR2->[${SUBVAR2}] ...."
done

# 3  
Old 05-05-2005
Hi vgersh99,
The VARIABLE IS COMING FROM CONFIG FILE AND AT ANY STAGE I DON'T KNOW HOW MANY VARIABLES I CAN DECLARE IN THE READ STATEMENT. THIS MUST BE DONE DYNAMICALLY.

JINGI
# 4  
Old 05-05-2005
Quote:
Originally Posted by jingi1234
Hi vgersh99,
The VARIABLE IS COMING FROM CONFIG FILE AND AT ANY STAGE I DON'T KNOW HOW MANY VARIABLES I CAN DECLARE IN THE READ STATEMENT. THIS MUST BE DONE DYNAMICALLY.

JINGI
Pls don't shout Smilie

Code:
#!/bin/ksh
nawk -F'|' '{ for(i=1; i <= NF; i++) printf("subvar%d=%s\n", i, $i)}' configFile

# 5  
Old 05-05-2005
hi vgersh99,
Sorry if you felt that I was shouting. As you can see I have just registered yesterday. Your reply works but not as expected as I wanted.

Now I am revising my question:

VARIABLE1="some1|some2|some3" (Comes from config and this variable may increase in future)
VARIABLE2="some4|some5|some6" (Comes from config and this variable may increase in future)

There is a one to one relation between the above variables (if i have to add a variable in VARIABLE1 then I will also add a variable in VARIABLE2).



LOOP START { from i =0 ; i<= {something in this case 3 but I don't know how to get 3 dynamically); i++

1: SomeCommand on VARIABLE1 --> should return some1 as a VARIABLE subvar1
2: SomeCommand on VARIABLE2 ---> should return some4 as a VARIABLE subvar2

function1 (irrelevant here) ---> returns a Value = value1
function2 (irrelevant here) ---> returns a Value = value2

if both value 1 and value 2 are not same
then
exit

}LOOP END

When it runs first time 1: retunes some1(as a subvar1) and 2: returns some4(as a subvar2), subvar1 and subvar2 will be available to function1 and function2 which in turn return value1 and value2 if both are not same then exit else continue for 2nd RUN in the loop

second time : 1: retunes some2 (as a subvar1) and 2: returns some5(as a subvar2), subvar1 and subvar2 will be available to function1 and function2 which in turn return value1 and value2 if both are not same then exit else continue for 3rd RUN in the loop.

and so on.....


Please help. Thanks in Advance.

Jingi
# 6  
Old 05-05-2005
assuming:
1. file 'config1' contains: 'some1|some2|some3'
2. file 'config2' contains: 'some4|some5|some6'

[not tested]

Code:
#!/bin/ksh

config1='config1'
config2='config2'

tr '|' '\n' < config1 > /tmp/$$_1
tr '|' '\n' < config2 > /tmp/$$_2
paste -d' ' /tmp/$$_1 /tmp/$$_2 | while read f1 f2
do
    echo "f1->[${f1}] f2->[${f2}]"

    if [ $(function1 "${f1}") != $(function2 "${f2}") ]; then
       exit
    fi;
done

\rm -rf /tmp/$$*

# 7  
Old 05-06-2005
Hi vgersh99,
I had slightly modified the script but I had used your idea. Thanks for your help. Smilie Smilie Smilie .

Thanks,
Jingi
 
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 separate a statement based on some delimiter and store each field in a variable?

Hi, Variable1 = MKT1,MKT2,MKT3,MKT4 Now i want to store each of these value seperated by comma to a array and access each of the values. Also find out number of such values seperated by comma. Variable1 can have any number of values seperated by comma. Thanks :) (3 Replies)
Discussion started by: arghadeep adity
3 Replies

2. Shell Programming and Scripting

Cut cmd with delimiter as |#|

Hi All- We have a file data as below with delimiter as |#| 10|#|20|#|ABC 13|#|23|#|PBC If I want to cut the 2nd field out of this, below command is not working as multiple pipe is causing an issue , it seems cut -f2 -d"|#|" <file_name> can you please help to provide the correct command... (7 Replies)
Discussion started by: sureshg_sampat
7 Replies

3. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

4. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

5. Shell Programming and Scripting

Cut columns with delimiter

HI, I have a file like below "103865","103835","Zming","","Zhu","103965","Sunnyvale","US", "116228","116227","Morlla","","Kowalski","113228","Paese "(Treviso)""IT" I want to validate the 7th column which is below. "Sunnyvale" "Paese In the above 7th column Paese is not ended with... (9 Replies)
Discussion started by: Krrishv
9 Replies

6. Shell Programming and Scripting

cut -d with more than 1 delimiter?

I need to cut or otherwise get the 4th and 5th position output of for i in `date +%H` ; do vnstat --dumpdb | grep "h;$i" ; done example output is: h;13;1310318701;443;93 I only need ";443;93" from any given run of "for i in `date +%H` ; do vnstat --dumpdb | grep "h;$i" ; done" Thanks... (3 Replies)
Discussion started by: Habitual
3 Replies

7. UNIX for Advanced & Expert Users

use a word as a delimiter with cut

Is there a way to use a word as a delimiter with cut? Or is there a way to use sed or awk with a word as a delimiter? I don't care which program I use for a delimiter I just want to use a word as a delimiter. (2 Replies)
Discussion started by: cokedude
2 Replies

8. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

9. Shell Programming and Scripting

Cut Number which appear before a delimiter

Hi All, How can i use the cut option to only output the number of rows, which is 2 is this case ? Pls note that the number of digit before the delimiter ":" is always varying. $ grep -n uuu xxx 2:** xxx yyy gg 44 tt uuu 2007 $ (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

\r as delimiter in cut

I need to use \r as a delimiter in the -d option of the cut comand . Any help ? Thanks in advance . SD (5 Replies)
Discussion started by: shweta_d
5 Replies
Login or Register to Ask a Question