Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can i use basename into awk ?

2,365 views
Skip to first unread message

Phraid

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to
Hie,
I ask to myself how we can use shell command (like basename, rm, ...) into a
AWK command ?
For example, i have this file named my_file

my_file contains
/bob/bob.sh|bob.sh is good
/users/my_user/my_script.sh|my_script.sh is good too
/users/bill/bill_files/bill_script.sh|bill_script.sh is really fine !!

I want to have (after few AWK lines) :
/bob/bob.sh|bob.sh|what bob.sh does
/users/my_user/my_script.sh|my_script.sh|what my_script.sh does
/users/bill/bill_files/bill_script.sh|bill_script.sh|bill_script.sh is
really fine !!

To do that, i have thought :

cat my_file | awk 'BEGIN{FS="|"}{print $1"|"basename $1"|"$2}'

because in a standard shell :
basename /bob/bob.sh returns bob.sh

But i don't know if it's possible to use "basename" (or another shell
command) into a AWK command ?
Could you help me for that ?
Or is exist another solution to resolv my problem ?
Thanks
Bye
phr...@bigfoot.com


Harlan Grove

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to

In the case of basename, keeping everything awk would be easier and quicker.
This sample script (stored for the purpose of this example with the filename
awkscript)

{ print $0 "\t\t" basename($0) }
function basename(pn) {
sub(/^.*\//, "", pn)
return pn
}

and run with the command

ls -r /users | awk -f awkscript

will print full pathnames and basenames.

More generally, try piping shell commands through getline. Like so:

{ s = $0; cmd = "some_shell_command " s; cmd | getline $0; close(cmd) }

This is rather inefficient because you'd be initiating and ending a process for
each input record. But I'm guessing at the ultimate structure of your script.
Note: you DO want to make sure you close your pipes.

bma...@iglou.com

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to

On 1999-07-02 phr...@bigfoot.com said:
>Hie,

>I ask to myself how we can use shell command (like basename, rm, ...
>) into a AWK command ?
>For example, i have this file named my_file
>my_file contains
>/bob/bob.sh|bob.sh is good
>/users/my_user/my_script.sh|my_script.sh is good too
>/users/bill/bill_files/bill_script.sh|bill_script.sh is really fine
>!!
>I want to have (after few AWK lines) :
>/bob/bob.sh|bob.sh|what bob.sh does
>/users/my_user/my_script.sh|my_script.sh|what my_script.sh does
>/users/bill/bill_files/bill_script.sh|bill_script.sh|bill_script.sh
>is really fine !!
>To do that, i have thought :
>cat my_file | awk 'BEGIN{FS="|"}{print $1"|"basename $1"|"$2}'
>because in a standard shell :
>basename /bob/bob.sh returns bob.sh
>But i don't know if it's possible to use "basename" (or another
>shell command) into a AWK command ?
awk 'BEGIN{FS="|";OFS=FS} {"basename " $1 | getline base;print $1,base,$2}' \
my_file
but it would be foolish to call an external program for something that can
be done in awk:
awk 'BEGIN{FS="|";OFS=FS} {base=$1;gsub(/.*\//,"",base);print $1,base,$2} my_file

Net-Tamer V 1.08X - Test Drive

Jim Monty

unread,
Jul 3, 1999, 3:00:00 AM7/3/99
to
bma...@iglou.com wrote:
> [...] but it would be foolish to call an external program for something

> that can be done in awk:
> awk 'BEGIN{FS="|";OFS=FS} {base=$1;gsub(/.*\//,"",base);print $1,base,$2} my_file

Only one substitution is needed to remove the leading path from
"base", so use sub(), not gsub(). And although the regular expression
engine of whatever awk is being used here likely has an internal
optimization that automagically prepends a ^ to any regular expression
pattern that begins with .*, it is always safer to explicitly write
your regular expressions the way you want them to match: in this
case, efficiently. It's important to anchor the pattern .* to the
beginning of the string to avoid a whole lot of unnecessary
backtracking and needless match attempts. Don't trust your awk's
regular expression engine to do this for you.

sub(/^.*\//, "", base)

--
Jim Monty
mo...@primenet.com
Tempe, Arizona USA

0 new messages