The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-11-2007
gratus gratus is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 5
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
  #2 (permalink)  
Old 10-11-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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>
  #3 (permalink)  
Old 10-11-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
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 (permalink)  
Old 10-11-2007
gratus gratus is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 5
yes, these worked. Thanks a lot.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0