0

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

I run the command 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 string (this string will always change but stays in the same place) and set it to a variable.

Originally I thought I could use grep or sed and print a line after each '|' and set the 13th line to a variable.

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

2
  • 1
    Is that all supposed to be on one line? Commented Aug 31, 2012 at 23:44
  • 1
    Yes, it is supposed to be on one line. Commented Sep 4, 2012 at 20:48

3 Answers 3

9

Using awk:

awk -F"|" '{print $NF}'

this will work:

echo " Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com | 
       e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A | 
       20120831 - daily" | awk -F"|" '{print $NF}'

and yield:

20120831 - daily

To assign to a variable (data.txt contains your string just for simplicity, it also works the echo above):

$ myvar=$(awk -F"|" '{print $NF}' data.txt)
$ echo $myvar
20120831 - daily

Explanation

the -F sets the input field separator, in this case to |. NF is a built-in awk variable that denotes the number of input fields, and the $ in front of the variable accesses that element, i.e., in this case the last field in the line ($NF).

Alternatively: You could grab each of the last three fields separated by white space (the awk default) with this:

awk '{print $(NF-2), $(NF-1), $NF}'
Sign up to request clarification or add additional context in comments.

6 Comments

@MichaelLe Updated the answer to show the assignment to a shell variable
@MichaelLe Happy to have been able to help.
I realized that I had a larger output and formatted my original question. I tried using $(NF-1) and such to grab the next fields, but it turns out that it is on the incorrect line. How would I go to the next line in the output? Thank you.
@MichaelLe That's a very different problem and really merits its own new question. It's one thing to grab part of a line (which is what you asked for originally) and then to have a different problem that consists of a multi-line (always? sometimes?) input where parts you are interested in are on potentially different lines (if I understood your comment). I would suggest you accept an answer that solved the problem you posted, and then carefully formulate another separate question to post new (including accurate input data and desired output). AWK, like other solutions here, is line-oriented.
Thank you, I didn't realize that there was more effort because it was multiline.
|
3

Levon's answer works great, but I just had to show there are always other ways with shell scripting.

This one uses the basic tool called cut

echo "Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com |  e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A |   20120831 - daily" | cut -d"|" -f13

2 Comments

Can you explain what the -f13 does?
If you have access to a Unix/Linux/*nix box, pull up a terminal and type man cut. In short, the -d"|" indicates that the | symbol separates fields, and the -f13 part asks for the 13th field. cut -d"|" -f13
0

I know that this has been accepted already, but here's how to do it in pure bash:

string="Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com | e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A | 20120831 - daily"
myvar="${string##*| }"
echo "$myvar"

3 Comments

Is there a reason to avoid Awk in this case? Awk is specified by POSIX and provided by Busybox. Smaller embedded systems don't have it, maybe? This is not a rhetorical question.
@ngks No, I just felt like pointing out that bash's internal string constructs are up to this particular job.
Cool, as @Levon said there are always other ways.

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.