Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

How to parse out fields using awk?

SOLVED
Go to Solution
Honored Contributor 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
1 ACCEPTED SOLUTION
Honored Contributor Honored Contributor

Re: How to parse out fields using awk?

man basename

live free or die
harry
Live Free or Die
15 REPLIES
Honored Contributor Honored Contributor

Re: How to parse out fields using awk?

man basename

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

Re: How to parse out fields using awk?

Hi,

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

Rgds.
Honored Contributor 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"
Honored Contributor 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
Honored Contributor 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
Honored Contributor 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
Honored Contributor 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
Honored Contributor 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!
Honored Contributor 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
Honored Contributor Honored Contributor

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!
Trusted Contributor Trusted Contributor

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.

Honored Contributor Honored Contributor

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

Honored Contributor

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
Highlighted
Respected Contributor Respected Contributor

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!
Honored Contributor Honored Contributor

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!