Hi I have a shell command like this.
s3=$(awk 'BEGIN{ print "S3 bucket path" }
/Executing command\(queryId/{ sub(/.*queryId=[^[:space:]]+: /,""); q=$0 }
/s3:\/\//{ print "," $10 }' OFS=',' hive-server2.log)
The output of the above command like this.
echo $s3
2018-02-21T17:58:22,
2018-02-21T17:58:26,
2018-02-21T18:05:33,
2018-02-21T18:05:34
I want to select the last line only. I need the last output like this.
2018-02-21T18:05:34
I tried like this.
awk -v $s3 '{print $(NF)}'
Not working.Any help will be appreciated.
In general,
command | tail -n 1
prints the last line of the output fromcommand
. However, wherecommand
is of the formawk '... { ... print something }'
you can refactor toawk '... { ... result = something } END { print result }'
to avoid spawning a separate process just to discard the other output.If you already have the result in a shell variable
s3
and want to print just the last line, a parameter expansionecho "${s3##*$'\n'}"
does that. The C-style string$'\n'
to represent a newline is a Bash extension, and the parameter expansion operator##
to remove the longest matching prefix isn't entirely portable either, so you should make sure the shebang line says#!/bin/bash
, not#!/bin/sh
Notice also that
$s3
without quotes is an error unless you specifically require the shell to perform whitespace tokenization and wildcard expansion on the value. You should basically always use double quotes around variables except in a couple of very specific scenarios.another approach can be, processing the file from the end and exiting after first match.
Hi you can do it just by adding
echo $s3 | sed '$!d'
It will simply print:-
Hope this will help you.