Solved

split into an array?, ksh, awk version

Posted on 2006-07-03
12
3,526 Views
Last Modified: 2013-12-26
How do you split a ksh shell variable into an array with awk?  

Perl version would be
@a = split /:/, $ENV{'PATH'}.

Thanks
0
Comment
Question by:hank1
  • 5
  • 4
  • 3
12 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 17032037
This should do it:

{
      path = ENVIRON["PATH"];
      cnt = split(path, dir, ":");

      for (i=0; i<cnt; i++)
            print dir[i];
}
0
 
LVL 1

Author Comment

by:hank1
ID: 17032222
I know this is wrong, but I want the array outside of awk.  Is the awk
struct even close?  :-)
Thanks  (I couldn't get your script to run since I don't know, really
don't know, awk.  I guessed at an invoke of just awk .... no.)



awk {
   path = ENVIRON["PATH"];
   cnt = split(path, dir, ":");
}

while [ ${cnt[index]} ] ; do
  echo Here's the element ${cnt[index]}
  index=$((index + 1))
}
0
 
LVL 1

Author Comment

by:hank1
ID: 17032311
Here's something I found, but it uses a file.  Tried your ENVIRON["PATH"] but didn't
go.  Wanted a file



Fiddling with IFS should have helped, though you also want to
double-quote the @ expansion in the for loop's control section:
  oIFS=$IFS
  IFS=${IFS#??}
  set -A array $(awk -F: '{print $3}' go.dat)
  IFS=$oIFS
  for job in "${array[@]}"; do
     echo "$job"
  done

0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17032339
It really depends on what you want to do with the array. Do you simply want to populate a shell array with the contents of PATH? In any case, to run awk the usual way is: awk -f scriptfile [datafile]
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17033021
I think it's easier to do it in the shell directly. From your use of set -A, I presume you're using ksh, so you can use:

#!/bin/ksh
# Set directory names into "dirs", but set spaces to "+" first
set -A dirs `echo $PATH | tr " " "+" | tr ":" " "`
i=0
while [ ! -z "${dirs[$i]}" ]
do
        # change the "+" to spaces
      dir=`echo ${dirs[$i]} | tr "+" " "`
      echo $dir
      i=`expr $i + 1`
done
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17033529
what's wrong with:
   set -A dirs `echo "$PATH"|tr ':' ' '`
# assuming that there is space in the path anywhere, which is unusal in Unix anyway
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:bpmurray
ID: 17033549
There are spaces in the path - that's the problem. As you've noted, it's easy with no spaces :-)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17033574
hmm, who said that there are spaces?
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17033621
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 125 total points
ID: 17035733
ok, then we need to do it in 2 steps as you suggested, tr only can't do it, but awk

   set -A dirs `echo "$PATH"|awk -F: '{for(i=1;i<=NF;i++){printf" '"'"'%s'"'"'",$i}}'`

# ugly, but you asked for it ;-)
0
 
LVL 1

Author Comment

by:hank1
ID: 17054704
Yep, works like a champ.  I'll have to dip into this awk some day.  Thanks.
0
 
LVL 1

Author Comment

by:hank1
ID: 17057739
strings windup with enclosed with 's.  
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now