Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Why not posted the awk codes you wish to merge? – manatwork Nov 4 '13 at 16:34
up vote 4 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


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.