0

I am running a command in CentOS that gives me an output of a multiple line string and I want to grab a certain part of that output and set it to a variable.

For example I run the commandline

ebi-describe-env

My output as follows:

ApplicationName | CNAME | DATECreated | DateUpdated | Description | EndpointURL |   
EnvironmentID | EnvironmentName | Health | Stack | Status | TemplateName | 
Version Label -------------------------------------
Web App | domain.com | 2012-02-23 | 2012-08-31 | |
anotherdomain.com | e-8sgkf3eqbj | Web-App-Name | Status | 
Linux | Ready | N/A | 20120831 - daily

I want to grab the '20120831-daily' part of the multi-string which is in the same place every call and set it to a variable. I believe the '------' means a new line or output.

I'm very new to bash scripting, so any help would be great. Thank you.

Note: I asked the question before and it was solved with awk, but it turned out it was only for a one line output. Previous question

2 Answers 2

2

You can easily add a pattern to match a particular line to the previous answer:

awk -F"|" 'NR == 6 {print $NF}'

The "pattern" for a block in awk can be any conditional. In my example NR is the line number, so that prints the last (pipe-separated) word on line 6. You could also use a pattern like /Linux/ if you want the line that has "Linux" in it.

Sign up to request clarification or add additional context in comments.

Comments

1

This should do the trick:

dateStr=$( ebi-describe-env | grep "Linux | Ready" | cut -t"|" -f4 )

Run the command and pipe its output through grep, which will only pass lines (well, the line) that contain the string "Linux | Ready". This is then passed to cut, which treats "|" as a delimiter and only prints the 4th field. The output is then captured by the $(...) construct and assigned to the variable dateStr.

Slightly better is Ben Jackson's awk solution, which can replace my grep/cut combination:

dateStr=$( ebi-describe-env | awk -F"|" 'NR==6 {print $NF}' )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.