I have a line :

Check space in www.raja.server.local.com and it is 40% full in /usr

I want to just parse www.raja.server.local.com from above line.

Note: www.raja.server.local.com can be any where in the line.

  • This question makes no sense unless the hostname changes with different lines. – h0tw1r3 Dec 9 '15 at 23:07
  • @h0tw1r3 Hello there , hostname will change but local.com will not change. It will exist in everyline but position of the local.com can be any where in the line – rɑːdʒɑ Dec 9 '15 at 23:10

If your question can be reworded as "print all space-separated fields ending in .local.com", then the following should do it (using GNU grep):

grep -Eo '[^ ]+\.local\.com\b'
  • No , everytime it will not be 4th field . – rɑːdʒɑ Dec 9 '15 at 23:03
  • I updated my answer to hopefully match your clarified question. – dhag Dec 10 '15 at 3:34
  • I will check and update you. – rɑːdʒɑ Dec 10 '15 at 4:20

Fragile, but it's an answer...

awk '/\.local\.com/ { x=gensub(/.* ([^\ ]+\.local\.com).*/,"\\1","1"); print x }'

A better solution would be to use back references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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