Operating System - HP-UX
1748282 Members
4120 Online
108761 Solutions
New Discussion юеВ

Re: How to parse out fields using awk?

 
SOLVED
Go to solution
Gino Castoldi_2
Honored Contributor

How to parse out fields using awk?

Hi,

I have a shell script that finds and lists the full path to a file. (The filename always changes) I need to parse out all fields including the forward slashes because I only need the filename at the end of the path.
ex. /var/opt/filename
What would be the best way to do this?

10 points ot any good answer.
TIA, Gino
15 REPLIES 15
harry d brown jr
Honored Contributor
Solution

Re: How to parse out fields using awk?

man basename

live free or die
harry
Live Free or Die
Jose Mosquera
Honored Contributor

Re: How to parse out fields using awk?

Hi,

Are you looking for the "basename" command?
#man basename

Rgds.
Mark Grant
Honored Contributor

Re: How to parse out fields using awk?

This is confusing to me.

Firstly "basename /var/opt/filename" will give you "filename". However, I don't understand why you would need to do this because presumably you already had the filename in order to find it.

Never preceed any demonstration with anything more predictive than "watch this"
Gino Castoldi_2
Honored Contributor

Re: How to parse out fields using awk?

Hi,

Mark: I'm using the 'find' command to locate the file so it returns the fullpath.

All: The 'basename' command looks like the solution to me.

10 points to any good answer.
TIA, Gino
harry d brown jr
Honored Contributor

Re: How to parse out fields using awk?

Also dirname returns the directory:

# filevar=/var/adm/syslog/syslog.log
# basename $filevar
syslog.log
# dirname $filevar
/var/adm/syslog
#

live free or die
harry
Live Free or Die
MarkSyder
Honored Contributor

Re: How to parse out fields using awk?

If you're using find, how about:

find / -name filename -exec basename {} \;

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Victor Fridyev
Honored Contributor

Re: How to parse out fields using awk?

Hi,

basename $FILE
and
dirname $FILE
are the best solution, but if you like awk:
PATH=$(echo $FILE|awk -F/ '{$NF="";print}' | sed 's? ?/?g'

HTH
Entities are not to be multiplied beyond necessity - RTFM
Fabio Ettore
Honored Contributor

Re: How to parse out fields using awk?

Hi Gino,

sorry, but the find command doens't already show the fullpath?
For example:

filename under /pippo/pluto
# find / -name filename
/pippo/pluto/filename

It returns fullpath then I don't understand your problem.
Otherwise I hope that other ITRC guys will help you better than me.

Best regards,
Ettore
WISH? IMPROVEMENT!
Gino Castoldi_2
Honored Contributor

Re: How to parse out fields using awk?

Hi,

Fabio: When I run the find command while searching for a particular file it returns the full path with the file itself.
I just need the file name by itself, I want to remove the rest of the string (which includes the full path).

10 points to any good answer.
TIA, Gino