How can I extract group-3-vm1 after I do this grep?

$ cat output/tpcds_query_1a_71_mr.out | grep "Tracking URL" | tail -1

Here's the result:

Starting Job = job_1442587212068_0126, Tracking URL = http://group-3-vm1:8088/proxy/application_1442587212068_0126/
share|improve this question
1  
Have you tried cut or awk? – thebjorn Sep 21 '15 at 21:41
    
What do you mean by 'extract'? Do you want the whole url or I've just misunderstood the question? – Mateusz Kleinert Sep 21 '15 at 21:47
up vote 3 down vote accepted

This awk command replaces the grep and tail commands while also extracting the text of interest:

$ awk -F'[:/]' '/Tracking URL/{n=$4;} END{print n;}' output/tpcds_query_1a_71_mr.out 
group-3-vm1

How it works

  • -F'[:/]'

    This sets the field separator to either a colon or a slash.

  • /Tracking URL/{n=$4;}

    This looks for lines containing Tracking URL and saves the fourth field in variable n.

  • END{print n;}

    After we have reached the end of the file, this prints the last n that we found.

Example

Here is a sample test file and output:

$ cat output/tpcds_query_1a_71_mr.out 
Starting Job = job_1442587212068_0126, Tracking URL = http://group-1-vm1:8088/proxy/application_1442587212068_0126/
Starting Job = job_1442587212068_0126, Tracking URL = http://group-2-vm1:8088/proxy/application_1442587212068_0126/
Starting Job = job_1442587212068_0126, Tracking URL = http://group-3-vm1:8088/proxy/application_1442587212068_0126/
Starting Job = job_1442587212068_0126, No Track URL = http://group-4-vm1:8088/proxy/application_1442587212068_0126/
$ awk -F'[:/]' '/Tracking URL/{n=$4;} END{print n;}' output/tpcds_query_1a_71_mr.out 
group-3-vm1
share|improve this answer

Try this:

cat output/tpcds_query_1a_71_mr.out | grep "Tracking URL" | tail -1 | sed 's/.\+Tracking URL = http:\/\/\([a-zA-Z0-9\-]\+\).*/\1/g'

Some explanation. This uses your solution but pipes the results through sed to strip everthing up to and include http:// and uses a group capture ((...)) to capture everything that's a-Z0-9 and -. NOTE - If some of your hosts have underscores, you'll need to add _ to the capture characters.

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.