Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2007
Registered User
 
Join Date: Oct 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces.

I tried the following concept test and it has problem with field 5 which contain a space, appearently so because "set -A" treats the space as a field delimiter.

Is there any other better way that the script can receive awk output and store them in an array with white spaces perserved?

TIA.


#!/bin/ksh

var="word1#word2|word3/word4|word5.word6|word7_word8|word9 word10|word11|word12"

set -A varray `echo "$var"| awk '{z=split($0,flds,"|")
for(i=1;i<=z;i++)
print flds[i]}'`

echo ${varray[0]}
echo ${varray[1]}
echo ${varray[2]}
echo ${varray[3]}
echo ${varray[4]}
echo ${varray[5]}
echo ${varray[6]}

---------
script output:

$test_read.ksh
word1#word2
word3/word4
word5.word6
word7_word8
word9
word10
word11
Sponsored Links
    #2  
Old 10-11-2007
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,657
Thanks: 164
Thanked 645 Times in 622 Posts
White spaces are the default field separator for awk

Code:
/home/jmcnama>  var="word1#word2|word3/word4|word5.word6|word7_word8|word9 word10|word11|word12"
/home/jmcnama> set -A arr `echo "$var"| awk -F'|' '{for(i=1; i<=NF; i++){printf("%s ", $i)}}'`
/home/jmcnama> echo ${arr[*]}
word1#word2 word3/word4 word5.word6 word7_word8 word9 word10 word11 word12
csadev:/home/jmcnama>

Sponsored Links
    #3  
Old 10-11-2007
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,383
Thanks: 112
Thanked 486 Times in 458 Posts
or a bit simpler:

Code:
#!/bin/ksh

var='word1#word2|word3/word4|word5.word6|word7_word8|word9 word10|word11|word12'

IFS='|'; set -A arr $(echo "$var")
i=0
while (( i <= 6 ))
do
    echo "i=>[$i] (${arr[$i]})"
    ((i=i+1))
done

    #4  
Old 10-11-2007
Registered User
 
Join Date: Oct 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
yes, these worked. Thanks a lot.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
split string into array in shell Roy987 Shell Programming and Scripting 5 05-16-2012 09:58 PM
Store values from a file into an array variable in Shell ezhil01 Shell Programming and Scripting 2 03-24-2012 11:19 AM
how to spilit a row into fields and store the field content to the array barani75 Shell Programming and Scripting 5 02-23-2010 01:50 AM
how to split the row(array) in to fields and store in to oracle database in unix barani75 Shell Programming and Scripting 3 02-22-2010 10:18 PM
how do I store the values in array using shell krishna UNIX for Advanced & Expert Users 2 08-17-2001 09:49 PM



All times are GMT -4. The time now is 02:05 AM.