![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | 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. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to split a field into two fields? | vbrown | Shell Programming and Scripting | 4 | 02-21-2008 06:50 AM |
| How to store the values in a file into an array | risshanth | UNIX for Dummies Questions & Answers | 3 | 01-22-2008 10:34 AM |
| How to store query multiple result in shell script variable(Array) | div_Neev | Shell Programming and Scripting | 4 | 11-06-2007 08:10 PM |
| Using varible/varible substitution in Perl/sed Search & Replace | Breen | Shell Programming and Scripting | 3 | 05-08-2002 05:45 AM |
| how do I store the values in array using shell | krishna | UNIX for Advanced & Expert Users | 2 | 08-17-2001 09:49 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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>
|
|
||||
|
yes, these worked. Thanks a lot.
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|