For Programmers: Free Programming Magazines  


Home > Archive > AWK > August 2005 > Print $PATH entries on separate lines









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 Print $PATH entries on separate lines
Sashi

2005-08-15, 4:59 pm

Hi, I'm trying to do the following on GNU Linux:
> echo $PATH | gawk -F: "{for (i=1;i<=NF;i++)print $i;}"


Rather than print each of the PATH entries on a separate line, it's
printing the entire PATH n times, where n is the number of entries in
PATH.
What am I doing wrong?
TIA,
Sashi

Ed Morton

2005-08-15, 4:59 pm

Sashi wrote:
> Hi, I'm trying to do the following on GNU Linux:
>
>
>
> Rather than print each of the PATH entries on a separate line, it's
> printing the entire PATH n times, where n is the number of entries in
> PATH.
> What am I doing wrong?


Use single quotes on UNIX, not double:

echo "$PATH" | gawk -F: '{for (i=1;i<=NF;i++)print $i}'

Ed.
Ed Morton

2005-08-15, 4:59 pm

Ed Morton wrote:

> Sashi wrote:
>
>
>
> Use single quotes on UNIX, not double:
>
> echo "$PATH" | gawk -F: '{for (i=1;i<=NF;i++)print $i}'


to continue.. because with double quotes, $i is being interpretted by
the shell and since it isn't set to anything, once the shell's finished
expanding "print $i" to awk looks like "print" which is the same as
"print $0".

Ed.
Chris F.A. Johnson

2005-08-15, 4:59 pm

On 2005-08-15, Sashi wrote:
> Hi, I'm trying to do the following on GNU Linux:
>
> Rather than print each of the PATH entries on a separate line, it's
> printing the entire PATH n times, where n is the number of entries in
> PATH.
> What am I doing wrong?


Use single quotes; $i is being interpreted as a shell variable:

echo $PATH | gawk -F: '{for (i=1;i<=NF;i++)print $i;}'


If this is all you want to do, you don't need awk; in bash or
ksh93:

printf "%s\n" ${PATH//:/ }


Or in any Bourne-type shell:

IFS=:
set -- $PATH
printf "%s\n" "$@" ## Or: for d do echo $d; done

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Bill Seivert

2005-08-16, 3:59 am



Sashi wrote:
> Hi, I'm trying to do the following on GNU Linux:
>
>
>
> Rather than print each of the PATH entries on a separate line, it's
> printing the entire PATH n times, where n is the number of entries in
> PATH.
> What am I doing wrong?
> TIA,
> Sashi
>


Would this work for you:

echo "$PATH" | tr ":" "\n"

Bill Seivert

Sashi

2005-08-16, 5:00 pm

Thanks, all! I learnt something new today, and my life is much the
richer for that.:-)
Sashi

Sponsored Links







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

Copyright 2008 codecomments.com