Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
echo -e 'one two three\nfour five six\nseven eight nine'
one two three
four five six
seven eight nine

how can I do some "MAGIC" do get this output?:

three
six
nine

UPDATE: I don't need it in this specific way, I need a general solution so that no matter how many columns are in a row, e.g.: awk always displays the last column.

share|improve this question
1  
Lance please research your questions before asking. Searching google for the subject line of your posts shows the answer in the snippents. Searching "awk last column" gives several great answers starting with result 1. Also, this 5 minute awk primer is worth reading all the way through so you know what's possible in the future. –  Caleb Jul 20 '11 at 18:11

7 Answers 7

up vote 2 down vote accepted

It can even be done only with 'bash', without 'sed', 'awk' or 'perl':

echo -e 'one two three\nfour five six\nseven eight nine' |
  while IFS=" " read -r -a line; do
    nb=${#line[@]}
    echo ${line[$((nb - 1))]}
  done
share|improve this answer
    
wow, nice one :) –  LanceBaynes Jul 20 '11 at 18:19
    
Hmm, or also, assuming your input is actually space-separated: ... | while read -r line; do echo ${line##* }; done –  glenn jackman Jul 20 '11 at 19:21
    
@glenn: This was my first idea, but when I read the 'read' manual, I saw this array function which I found useful. It can also be easily modified to give any field indexed from the right. –  jfgagne Jul 21 '11 at 7:54
1  
bash array index is subject of arithmetic evaluation, so echo ${line[nb - 1]} is enough. As speaking about bash, you can just skip the “nb” things: echo ${line[-1]}. A more portable alternative of the later: echo ${line[@]: -1]}. (See Stephane Chazelas' comment on negative indexes elsewhere.) –  manatwork Mar 18 '13 at 12:04

echo -e 'one two three\nfour five six\nseven eight nine' | awk '{print $3}'

EDIT:

echo -e 'one two three\nfour five six\nseven eight nine' | awk '{print $NF}'

share|improve this answer
    
I updateted the Q –  LanceBaynes Jul 20 '11 at 17:28
    
please note that awk is limited to 99 fields... :/ This just bite me in the past days ( ps -ef | awk '{ print $NF }' had some lines truncated...) Perl doesn't have that limitation. ( gnu.org/software/autoconf/manual/autoconf-2.67/html_node/… : "Traditional Awk has a limit of 99 fields in a record. Since some Awk implementations, like Tru64's, split the input even if you don't refer to any field in the script, to circumvent this problem, set ‘FS’ to an unusual character and use split." ) –  Olivier Dulac Jan 29 at 8:48

It can also be done using 'sed':

echo -e 'one two three\nfour five six\nseven eight nine' | sed -e 's/^.* \([^ ]*\)$/\1/'

Update:

or more simply:

echo -e 'one two three\nfour five six\nseven eight nine' | sed -e 's/^.* //'
share|improve this answer
    
more simply is better! –  mdpc Jul 20 '11 at 21:45
    
@mdpc: I agree, but as the more complicated solution was already posted, I decided to keep it. –  jfgagne Jul 21 '11 at 7:50
... | perl -lane 'print $F[-1]'
share|improve this answer

Or using cut:

echo -e 'one two three\nfour five six\nseven eight nine' | cut -f 3 -d' '

although this does not satisfy the 'general solution' requirement. Using rev twice we can solve this as well:

echo -e 'one two three\nfour five six\nseven eight nine' | rev | cut -f 1 -d' ' | rev
share|improve this answer
    
I do not think 'rev' can be found on all Unix (AIX, Solaris, ...) or is installed on all Linux, but nice alternative solution. –  jfgagne Jul 21 '11 at 8:34
    
+1 for double rev, but as a side note, rev does not work with 'wide' characters, only single byte ones, as far as I know. –  Marcin Jul 19 '12 at 17:47

It's easier than you think.

$ echo one two three | awk '{print $NF}'
three
share|improve this answer

Using awk you can first check if there is at least one column.

echo | awk '{if (NF >= 1) print $NF}'

echo 1 2 3 | awk '{if (NF >= 1) print $NF}'
share|improve this answer
    
Or less verbosely awk 'NF{print $NF}'. –  manatwork Mar 18 '13 at 9:17

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.