Sign up ×
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?:


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
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

9 Answers 9

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
    echo ${line[$((nb - 1))]}
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
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}'


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. (… : "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 '14 at 8:48

It's easier than you think.

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

It can also be done using 'sed':

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


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

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

In perl this can be done as follows:


#create a line of arbitrary data
$line = "1 2 3 4 5";

# splt the line into an array (we call the array 'array', for lolz)
@array = split(' ', $line);

# print the last element in the array, followed by a newline character;
print "$array[-1]\n";


$ perl

You could also loop through a file, heres an example script I wrote to parse a file called budget.dat

example data in budget.dat:

Rent              500
Food              250
Car               300
Tax               100
Car Tax           120
Mag Subscription  15

(you can see i needed to capture only the "last" column, not column 2 only)

The script:

$budgetfile = "budget.dat";
open($bf, $budgetfile)
        or die "Could not open filename: $filename $!";

print "-" x 50, "\n";
while ( $row = <$bf> ) {
        chomp $row;
        @r = split (' ', $row);
        print "$row ";
        $subtotal += $r[-1];
        print "\t$subtotal\n";
print "-" x 50, "\n";
print "\t\t\t Total:\t$subtotal\n\n";
share|improve this answer
I realised someone else already commented the same, sorry about that, at least I have a few examples too, hopefully it adds to the discussion anyway. –  urbansumo Apr 29 at 16:01
echo -e 'one two three\nfour five six\nseven eight nine' |awk -F " " '{print $NF }'
share|improve this answer
This adds nothing to the existing awk answers... –  jasonwryan May 22 at 8:07

Your Answer


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.