BaSH: Creating an array based on delimiters

Need help with C, C++, perl, python, etc?

BaSH: Creating an array based on delimiters

Postby MadSc13ntist » 2007-04-08 02:59

Does someone know of a simple way to do this in BaSH?

example:

creating an array from the PATH (environment) variable.
Code: Select all
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games


Once split into sections (indicated by ":", the delimiter) into array would create:
Code: Select all
array[0]=5 (number of elements in the arrray)
array[1]=/usr/local/bin
array[2]=/usr/bin
array[3]=/bin
array[4]=/usr/bin/X11
array[5]=/usr/games


I'm sure there must be a painfully simple process for this but at present it eludes me... props to anyone that can help point me in the right direction.
o)}|: MadSc13ntist :|{(o
Debian lenny :: AMD64 X2 Duo :: MSI K8NG82 :: Nvidia 7600 GS :: Gnome :: Compiz-fusion
User avatar
MadSc13ntist
 
Posts: 115
Joined: 2005-07-23 16:34

Slow Progress

Postby MadSc13ntist » 2007-04-08 04:17

I've made some progress.

I know that i can pull out specific elements using awk:
Code: Select all
echo $PATH | awk -F : '{print $1}'


Where $1 represents the first element (not the first parameter) a'la:
Code: Select all
madsc13ntist@madhatter:~/Desktop$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
madsc13ntist@madhatter:~/Desktop$ echo $PATH | awk -F : '{print $1}'
/usr/local/bin
madsc13ntist@madhatter:~/Desktop$
o)}|: MadSc13ntist :|{(o
Debian lenny :: AMD64 X2 Duo :: MSI K8NG82 :: Nvidia 7600 GS :: Gnome :: Compiz-fusion
User avatar
MadSc13ntist
 
Posts: 115
Joined: 2005-07-23 16:34

another question

Postby MadSc13ntist » 2007-04-08 04:26

Does anyone know how to substitute the $1 in this line with a variable?

Code: Select all
echo $StringSplit_String | awk -F$StringSplit_Delim '{print $1}'


It appears to be one of the last hangups i have right now.

Thanks for anyone that helps me out.
o)}|: MadSc13ntist :|{(o
Debian lenny :: AMD64 X2 Duo :: MSI K8NG82 :: Nvidia 7600 GS :: Gnome :: Compiz-fusion
User avatar
MadSc13ntist
 
Posts: 115
Joined: 2005-07-23 16:34

Great awk tutorial page

Postby MadSc13ntist » 2007-04-08 05:30

This page has been a HUGE help teaching me awk.

I think i am going to be using awk ALOT more, thinking about kicking cut to the curb! lol! :D

Still hopeful that someone take pitty on me and maybe help me out here... can anyone tell me a way to get the number of elements possible for each line awk returns?

using the same example with breaking up the PATH variable. Is there a way to return the number of elements (NULL or not).

As if I was trying to only return the LAST element in the array/hash
o)}|: MadSc13ntist :|{(o
Debian lenny :: AMD64 X2 Duo :: MSI K8NG82 :: Nvidia 7600 GS :: Gnome :: Compiz-fusion
User avatar
MadSc13ntist
 
Posts: 115
Joined: 2005-07-23 16:34

awk

Postby norm » 2007-04-08 17:08

I have been curious about awk for a while but I haven't taken the time to try and learn it. Your posts piqued my interest and the following is what I have learned:

Using the your example the $PATH variable
Code: Select all
echo $PATH |awk -F : '{for(i = 0 ; i <= NF ; i++) print $i }'

Yields:
Code: Select all
/usr/local/qt/bin
/usr/local/bin
/usr/bin
/bin
/usr/bin/X11
/usr/games

NF is the number of fields in a record, so:
Code: Select all
echo $PATH |awk -F : '{print NF }'

will output the number of fields (in this case 6) and
Code: Select all
echo $PATH |awk -F : '{print $NF }'

will print the last field:
Code: Select all
/usr/games


Don't know whether this helps you or not but I thought I would post the results of my trial(s) and error(s).

Cheers,
Norm
norm
 

Postby drl » 2007-04-09 04:41

Hi.

One way in bash, using a loop construct:
Code: Select all
#!/bin/sh

# @(#) s1       Demonstrate array setting.
# See http://www.tldp.org/LDP/abs/html/arrays.html for details on
# array notation.
# See http://www.tldp.org/LDP/abs/html/internalvariables.html for
# information about IFS.


sh --version

debug="echo"
debug="true"

SEQ=/usr/bin/seq

oldifs="$IFS"
IFS=":"

j=0
for i in $PATH
do
  a[$j]=$i
  $debug " set item $j, ${a[$j]}"
  j=$(( j+1 ))
done

IFS="$oldifs"
for i in $($SEQ 0 $((${#a[@]} - 1)))
do
  echo ${a[$i]}
done

exit 0

Which produces:
Code: Select all
% ./s1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
/home/drl/executable
/home/drl/bin
/home/drl/p/stm/plat/i686-pc-linux-gnu/scripts
/home/drl/p/stm/common/scripts
/usr/local/bin
/usr/bin
/bin
/usr/X11R6/bin

cheers, drl
Last edited by drl on 2007-04-09 05:08, edited 1 time in total.
["Sure, I can help you with that." -- USBank voice recognition system.
( Mn, 2.6.x, AMD-64 3000+, ASUS A8V Deluxe, 3 GB, SATA + IDE, NV34 )
Debian Wiki | Packages | Backports | Netinstall
User avatar
drl
 
Posts: 434
Joined: 2006-09-20 02:27
Location: Saint Paul, Minnesota, USA

Postby drl » 2007-04-09 05:07

Hi.

Using the array assignment feature:
Code: Select all
#!/bin/sh

# @(#) s2       Demonstrate array setting.
# See http://www.tldp.org/LDP/abs/html/arrays.html for details on
# array notation.
# See http://www.tldp.org/LDP/abs/html/internalvariables.html for
# information about IFS.

sh --version

debug="echo"
debug="true"

SEQ=/usr/bin/seq

oldifs="$IFS"
IFS=":"

a=( $PATH )

IFS="$oldifs"
$debug " Number of elements in array is $((${#a[@]}))"
for i in $($SEQ 0 $((${#a[@]} - 1)))
do
  echo ${a[$i]}
done

exit 0

Producing:
Code: Select all
% ./s2
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
/home/drl/executable
/home/drl/bin
/home/drl/p/stm/plat/i686-pc-linux-gnu/scripts
/home/drl/p/stm/common/scripts
/usr/local/bin
/usr/bin
/bin
/usr/X11R6/bin

cheers, drl
["Sure, I can help you with that." -- USBank voice recognition system.
( Mn, 2.6.x, AMD-64 3000+, ASUS A8V Deluxe, 3 GB, SATA + IDE, NV34 )
Debian Wiki | Packages | Backports | Netinstall
User avatar
drl
 
Posts: 434
Joined: 2006-09-20 02:27
Location: Saint Paul, Minnesota, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable