For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > An array for loop









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author An array for loop
Hon Seng Phuah

2004-05-24, 4:30 am

I have a statement combination of csh and awk:

filePath = "/test/good/life"
echo $filePath | awk '{ split($0, subdir, "/"); for (path in subdir)
filename = subdir[path]; print filename}'

if the above statements it runs, it does not print anything. If I
change the subdir[path] to subdir[4], it displays life.

Now, my problem is that the variable, filePath, is not fixed during
the run time. If I want to split filePath variable string and get a
word after the last character, "/". For the above example, I want to
get the word, life. How shall I modiify my above statement? Note:
During the run time, the variable, filePath, has at least one
character, "/".

Thanks.

-HS
Stefan Lagotzki

2004-05-24, 5:30 am

Hon Seng Phuah <hsphuah@usa.com>:
> filePath = "/test/good/life"
> echo $filePath | awk '{ split($0, subdir, "/"); for (path in subdir)
> filename = subdir[path]; print filename}'
>
> if the above statements it runs, it does not print anything. If I
> change the subdir[path] to subdir[4], it displays life.


You may set '/' as field-separator:

echo $filePath | awk -F'/' '{for (i=2;i<=NF;i++) {print $i}}'
echo $filePath | awk -F'/' '{print $NF}'

Test:

stefan2@platon:~/test> filePath="/test/good/life"
stefan2@platon:~/test> echo $filePath | awk -F'/' '{for (i=2;i<=NF;i++)
{print $i}}'
test
good
life
stefan2@platon:~/test> echo $filePath | awk -F'/' '{print $NF}'
life
Manuel Collado

2004-05-24, 6:30 am

Hon Seng Phuah wrote:
> I have a statement combination of csh and awk:
>
> filePath = "/test/good/life"
> echo $filePath | awk '{ split($0, subdir, "/"); for (path in subdir)
> filename = subdir[path]; print filename}'
>
> if the above statements it runs, it does not print anything. If I
> change the subdir[path] to subdir[4], it displays life.


You should put braces around the 'for' action:

filePath = "/test/good/life"
echo $filePath | awk '{ split($0, subdir, "/"); for (path in subdir) {
filename = subdir[path]; print filename} }'

Without braces, the 'for' action is null (newlines terminate sentences)

>
> Now, my problem is that the variable, filePath, is not fixed during
> the run time. If I want to split filePath variable string and get a
> word after the last character, "/". For the above example, I want to
> get the word, life. How shall I modiify my above statement? Note:
> During the run time, the variable, filePath, has at least one
> character, "/".
>
> Thanks.
>
> -HS


--
To reply by e-mail, please remove the extra dot
in the given address: m.collado -> mcollado

Ed Morton

2004-05-24, 9:30 am



Manuel Collado wrote:
> Hon Seng Phuah wrote:
>
>
>
> You should put braces around the 'for' action:
>
> filePath = "/test/good/life"
> echo $filePath | awk '{ split($0, subdir, "/"); for (path in subdir) {
> filename = subdir[path]; print filename} }'
>
> Without braces, the 'for' action is null (newlines terminate sentences)


No,it's attempting to set "filename" to be the last-index substring in
"subdir" which is probably exactly what the OP wants to print out since
his variable is called "filename".

The problem is that, annoyingly enough, the "in" operator doesn't
guarantee to go through the loop indices in first-to-last order, so even
though the "subdir" array gets populated such that "life" is in the last
(4th) field, the "for path in" loop might assign "path" to the values 4
then 2 then 1 then 3 or some other order other than the 1, 2, 3, 4 that
the OP wants. In the OPs case since he says that nothing prints out, the
final index beign generated must be "1" which would hold the null
substring since there's a slash right at the start of the original
"filePath". The OP could test that by sticking some character at the
start of "filePath" and see it get printed out.

So, the best solution is to use "/" as the field separator as Stefan
suggested elsethread:

> echo $filePath | awk -F'/' '{for (i=2;i<=NF;i++) {print $i}}'
> echo $filePath | awk -F'/' '{print $NF}'


or, if you want to use "split", then just save and use it's return value:

awk '{c=split($0,subdir,"/");for (i=2;i<=c;i++) {print $i}}'
awk '{c=split($0,subdir,"/");print $c}'

Regards,

Ed.

pop

2004-05-24, 11:30 am

"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:bZKdnR6L8MpZeyzdRVn-tw@comcast.com...
>
>
>
> No,it's attempting to set "filename" to be the last-index substring in
> "subdir" which is probably exactly what the OP wants to print out since
> his variable is called "filename".
>
> The problem is that, annoyingly enough, the "in" operator doesn't
> guarantee to go through the loop indices in first-to-last order, so even
> though the "subdir" array gets populated such that "life" is in the last
> (4th) field, the "for path in" loop might assign "path" to the values 4
> then 2 then 1 then 3 or some other order other than the 1, 2, 3, 4 that
> the OP wants. In the OPs case since he says that nothing prints out, the
> final index beign generated must be "1" which would hold the null
> substring since there's a slash right at the start of the original
> "filePath". The OP could test that by sticking some character at the
> start of "filePath" and see it get printed out.
>
> So, the best solution is to use "/" as the field separator as Stefan
> suggested elsethread:
>
>
> or, if you want to use "split", then just save and use it's return value:
>
> awk '{c=split($0,subdir,"/");for (i=2;i<=c;i++) {print $i}}'
> awk '{c=split($0,subdir,"/");print $c}'


I think the above should be:
awk '{c=split($0,subdir,"/");print subdir[c]}'


>
> Regards,
>
> Ed.
>

--
pop is Mark
I'm lost. I've gone to look for myself.
If I should return before I get back, keep me here.
--


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com