0

I have an attribute that contains multiple words.

Example:

0x1230ac1="Brown Fox is Near"

I am able to get the contents of the attribute by typing a command (using a CLI interface) like this:

show attr=0x1230ac1

The output of this "show" command looks like this:

ID            Name                  Value
0x1230ac1     User_Attribute1       Brown Fox is Near

If I use this same "show" command in a bash script I can do this:

show attr=0x1230ac1 | sed 1d | awk '{print $1,$3,$4,$5,$6}'

This is problematic because the string in the Value column might have 2 words or 20 words. I don't want to do an awk for each.

Is there a way to awk or sed or other which will pass the entire contents of 'Value' so that in my CLI script it will display all words that are contained within 'Value' ? This is important because I need to pass 'Value' to a string variable within the script.

Example:

MYSTRING=`show attr=0x1230ac1 | sed 1d | awk '{print $3-$99}' `

I know that the print $3-$99 is incorrect. It's just the example I am trying to do without having to put every possible print number.

2 Answers2

3

In awk you could remove some of the fields/columns by assigning an empty string to them:

$ echo foo bar doo | awk '{$2 = ""; print}'
foo  doo

However, that leaves an empty space where the field used to be, and collapses other whitespace in the line, like the space before asdf here:

$ echo "foo bar doo    asdf"  | awk '{$2 = ""; print}'
foo  doo asdf

Instead, you could do something with sed and an appropriate regex:

$ echo '0x1230ac1     User_Attribute1       Brown Fox is Near' |
  sed -Ee 's/([^[:space:]]+[[:space:]]+){2}//'
Brown Fox is Near

[^[:space:]]+ matches a run of non-whitespace characters, [[:space:]]+ a run of whitespace, and the surrounding (...){2} does that twice so anything up to the second run of whitespace is removed. That should work if the whitespace is spaces or tabs.

So, if I got your situation right, this should do:

$ show attr=0x1230ac1 | sed -E -e 1d -e 's/([^[:space:]]+[[:space:]]+){2}//'
ilkkachu
  • 138,973
0

You can use gnu sed for this:

sed -nEe '2s/\S+/\n&/3;s/.*\n//p'

Assumes that variable has no newlines. And the user id is whitespace free.