Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to execute awk on something like this :

<tag x="whatever" y="http://mysite.com/somewords/word1/word2/word3/word4">

I would like to extract word2 which comes forever after word1, knowing that somewords does not always exist.

I was able to do it using awk, but I used it 3 times, each time with one delimiter.

Is there any way to merge multiple awk commands with multiple delimiters, or simply one awk command to pick out word2?

share|improve this question
1  
Why not posted the awk codes you wish to merge? –  manatwork Nov 4 '13 at 16:34

1 Answer 1

up vote 3 down vote accepted

The awk delimiter can be a regular expression, so if you want to split the line on, say, spaces and slashes, you can use -F '[ /]'

But, given your sketchy description, you may just need this:

awk -F/ '{for (i=1; i<NF; i++) if ($i == "word1") {print $(i+1); break}}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.