|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
yes, these worked. Thanks a lot.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|