1753954 Members
7751 Online
108811 Solutions
New Discussion юеВ

Re: how to parse a list

 
SOLVED
Go to solution
Sachin_29
Advisor

how to parse a list

Hi:
I am reading a path A/B/C/D/.... froma file .
and creating a directory srtucture starting with subdirectory C under my home (~me/C/D/...)
In this path, the number of subdirectories could vary . Is there any way I can check how many subdirectories are there under and create those direcotroes dynamically thru the script?
6 REPLIES 6
Dave Olker
HPE Pro

Re: how to parse a list

Hi Sachin,

I'm not exactly sure if this is what you're asking, but you could always use the -p option of mkdir and it will create as many directories and subdirectories as you specify in a single command.

For example:

# ls /A
/A not found

# mkdir -p /A/B/C/D/E/F/G
# ll /A/B/C/D/E/F
total 0
drwxrwxrwx 2 root users 96 Aug 25 11:02 G

So with one command I created an entire directory hierarchy, regardless of the number of subdirectories.

Again, not sure if this is what you were looking for, but thought it might help.

Regards,

Dave
I work for HPE

[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Sachin_29
Advisor

Re: how to parse a list

dave:
I am reading a path from a file in the file I have A/B/C/D in the file. from the file i have to read and create directories in my home.. hope i m not confusing
Hoefnix
Honored Contributor
Solution

Re: how to parse a list

If you want to make a path of multiple directories it's symple us:
mkdir -p ~me/C/D/P/Z

But if you like to know the nummer of directories that need to be created reading from the file you can use awk

echo "A/B/C/D" | awk -F / '{print NF}'

HTH (if I understand you correct?),

Peter
Rodney Hills
Honored Contributor

Re: how to parse a list

If you need to strip off the "A/B/" and pick up the directory create at C, then

$ x="A/B/C/D"
$ y=${x#A/B/}
$ echo $y
C/D
$ mkdir -p $y

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: how to parse a list

If you are looking to break the path apart, then you do the following under "ksh".

$ IFS="/" ; b=A/B/C/D ; set -A a $b ; echo ${a[0]} ${#a[*]}

This will print "A" (for ${a[0]}) and "4" (for ${#a[*], number of elements).

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: how to parse a list

Sachin,

Assigning of points wouldn't offend any of us :-)

...
There be dragons...