Operating System - HP-UX
1833016 Members
2746 Online
110048 Solutions
New Discussion

how to search for field in awk seperated by '/'

 
SOLVED
Go to solution
Henry Chua
Super Advisor

how to search for field in awk seperated by '/'

Hi Guys,

with awk you can search for field seperated by space, can I used it or any other method to search for field seperated by "/"..
e.g. /HOST1/HOST/2/HOST3

thanks!!
Henry
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how to search for field in awk seperated by '/'

Sometimes you really should use the man command. Man awk would tell you about the -F option (field separator).

echo "HOST1/HOST2/HOST3" | awk -F '/' '{print $2}'
If it ain't broke, I can fix that.
Ravi_8
Honored Contributor

Re: how to search for field in awk seperated by '/'

Hi

awk -F''

field seperator no need to be space, it can be anything
never give up
Thierry Poels_1
Honored Contributor

Re: how to search for field in awk seperated by '/'

hi,

cut -d'/' -f3 yourfile

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Muthukumar_5
Honored Contributor

Re: how to search for field in awk seperated by '/'

You can use -F (field separator) as suggested by all.

echo /HOST1/HOST/2/HOST3 | awk -F "/" '{ print $2 }'

Else you tr command to change / to ' ' to use awk as default :) as,

echo /HOST1/HOST/2/HOST3 | tr '/' ' ' | awk '{ print $2 }'

HTH.
Easy to suggest when don't know about the problem!
Chris Vail
Honored Contributor

Re: how to search for field in awk seperated by '/'

As others have pointed out, the field separator argument can be used:
awk -F
An alternative way of denoting this is awk -F\/ '{print $2}'
Okay, it saves a grand total of ONE keystroke over awk -F "/" '{print $2}', but that is THE UNIX WAY!!!!!!!!!!!
[The \ is the "escape character", and tells awk that you really do want the next character, the / used as a character rather than as some form of another argument].

This comes in handy (and delightfully obscure) when using sed.
For example:
cat myfile|sed 's/\\/\//'
In sed speak, this means "swap literally the slash character (/) with literally the backslash character (\)".


Chris