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
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.
Net-Tamer V 1.08X - Test Drive
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