1

I have next issue:

Look at the structure information:

0   PATRAC_1  False   Normal    Normal  3     Not configure

We have seven columns, seven values, so, I need to get that values using awk, at the beginig I use space like separator, so, if I put print $1, I get: "0", if I put print $2, I get "PATRAC_1", etc.

But when I try to get column 7, I get only: "Not", because space between "not" and "configure"

So, what can I do to solve it?

6
  • 1
    Dont use a single space as the delimiter, how did you expect it to work when there is a space in the middle of the fields ?
    – 123
    Jun 26 '15 at 15:05
  • You don't have 7 columns. You could cheat and use read's greediness with 7 variables, which would put words 7 and beyond into variable 7.
    – Jeff Schaller
    Jun 26 '15 at 15:15
  • The point is that I need to get "Not configure" like a complete value Jun 26 '15 at 15:18
  • You've hit a dead end with awk and spaces. Are you stuck with awk? Can you influence the input before it gets to this point? Do you see the problem?
    – Jeff Schaller
    Jun 26 '15 at 15:40
  • No, I can not see it. Could you give me some example? thank you Jun 26 '15 at 16:22
3

If your data is at fixed columns, you can ask awk to split at those column widths by setting FIELDWIDTHS. eg:

awk '
 BEGIN{ FIELDWIDTHS="4 10 8 10 8 6 99" }
 { printf "1=%s 2=%s 7=%s\n", $1, $2, $7 }
'

gives

1=0    2=PATRAC_1   7=Not configure     
1
  • FIELDWIDTHS is specific to GNU awk.
    – lcd047
    Jun 26 '15 at 17:44
2

By default, awk considers any sequence of whitespace to be a column separator. It can't guess that you want that 7th column to span until the end of the line including embedded whitespace, you need to tell it.

Since you have variable amounts of whitespace between columns, keep the whitespace-based definition of columns. But instead of using $7 for the last column, use the text of the line, minus the first six columns.

awk '{
    last_column = $0;
    sub(/^[[:blank:]]*([^[:blank:]]+[[:blank:]]+){6}/, "", last_column);
    print last_column;
}'

If you have an old version of awk that doesn't support character classes, use [ \t] instead of [[:blank:]]. If you have an old version of awk that doesn't support braces in regular expressions, make 6 copies of that regexp.

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.