Re: How to parse out fields using awk? (607 Views)
Reply
Honored Contributor
Gino Castoldi_2
Posts: 1,365
Registered: ‎07-28-2000
Message 1 of 16 (606 Views)
Accepted Solution

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
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 2 of 16 (606 Views)

Re: How to parse out fields using awk?

man basename

live free or die
harry
Live Free or Die
Please use plain text.
Honored Contributor
Jose Mosquera
Posts: 1,013
Registered: ‎06-27-2002
Message 3 of 16 (606 Views)

Re: How to parse out fields using awk?

Hi,

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

Rgds.
Please use plain text.
Honored Contributor
Mark Grant
Posts: 2,712
Registered: ‎04-01-2003
Message 4 of 16 (606 Views)

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"
Please use plain text.
Honored Contributor
Gino Castoldi_2
Posts: 1,365
Registered: ‎07-28-2000
Message 5 of 16 (606 Views)

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
Please use plain text.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 6 of 16 (606 Views)

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
Please use plain text.
Honored Contributor
MarkSyder
Posts: 1,815
Registered: ‎11-10-2003
Message 7 of 16 (606 Views)

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
Please use plain text.
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 8 of 16 (606 Views)

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
Please use plain text.
Honored Contributor
Fabio Ettore
Posts: 1,738
Registered: ‎11-19-2003
Message 9 of 16 (606 Views)

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!
Please use plain text.
Honored Contributor
Gino Castoldi_2
Posts: 1,365
Registered: ‎07-28-2000
Message 10 of 16 (606 Views)

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
Please use plain text.
Honored Contributor
Fabio Ettore
Posts: 1,738
Registered: ‎11-19-2003
Message 11 of 16 (607 Views)

Re: How to parse out fields using awk?

Ok, now I understand your problem.
I saw your first post but the second one diverted me:

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

No points for me....

Best regards,
Ettore
WISH? IMPROVEMENT!
Please use plain text.
Trusted Contributor
Steve Post
Posts: 1,058
Registered: ‎09-13-1998
Message 12 of 16 (607 Views)

Re: How to parse out fields using awk?

Ok.....
find /mydir -print | xargs basename?
NO. THAT won't work.
How about...
find /mydir | awk -F/ '// {print $NF}'
Yep. That looks right. I saw the awk command in the forums above.

But if you are looking for the base name perhaps you are just looking for the file?
If that's the case....."I want to find a file that begins with "billy".
find /mydir -name "billy*" -print | awk -F/ '// {print $NF}'

In awk the -F/ is means separate the fields by the / character.
The $NF means the current number of fields on that line.
So print $NF means print the last field on the line.

Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 13 of 16 (607 Views)

Re: How to parse out fields using awk?

basename and dirname do a great job in parsing just the name and the path respectively. However, the shell already knows how to do this so you can bypass awk, sed and basename with something like this:

find /somewhere | while read
do
echo ${REPLY##*/}
done

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 14 of 16 (607 Views)

Re: How to parse out fields using awk?

# perl -MFile::Find -MCwd=abs_path 'find (sub { my $filename = $_ ; my $relative_name = $File::Find::name; my $fullname = abs_path ($_) }, @ARGV)' dir dir dir

Modify to your liking

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Please use plain text.
Respected Contributor
Tom Maloy
Posts: 221
Registered: ‎04-07-2002
Message 15 of 16 (607 Views)

Re: How to parse out fields using awk?

If you are using ksh, you can use variable manipulation.

If the variable is stored in variable "fn", try this:

echo ${fn##*/}

and it should show you just the file name.

The "##" removes the large left pattern.
The "*/" is the pattern to be removed.

Tom
Carpe diem!
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 16 of 16 (607 Views)

Re: How to parse out fields using awk?

Hai.

You can get the directory name and pathname of all regular files using,

#!/usr/bin/ksh

echo " DIR-NAME FILE-NAME"
find / -type f | while read file; do
echo "`dirname $file` `basename $file`"
done

For other files like character files,block devices change the -type X value from man find

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation