Operating System - HP-UX
1752701 Members
6763 Online
108789 Solutions
New Discussion юеВ

Re: How to parse out fields using awk?

 
SOLVED
Go to solution
Fabio Ettore
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!
Steve Post
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.

Bill Hassell
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



Bill Hassell, sysadmin
H.Merijn Brand (procura
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
Tom Maloy
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!
Muthukumar_5
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!