how to select the last line of the shell output

2020-05-06 10:42发布

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.

标签: bash shell awk sed
3条回答
Ridiculous、
2楼-- · 2020-05-06 11:03

In general, command | tail -n 1 prints the last line of the output from command. However, where command is of the form awk '... { ... print something }' you can refactor to awk '... { ... 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 expansion echo "${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.

查看更多
太酷不给撩
3楼-- · 2020-05-06 11:13

another approach can be, processing the file from the end and exiting after first match.

tac file | awk '/match/{print; exit}'
查看更多
你好瞎i
4楼-- · 2020-05-06 11:21

Hi you can do it just by adding echo $s3 | sed '$!d'

s3=$(awk 'BEGIN{ print "S3 bucket path" }/Executing command\(queryId/{ sub(/.*queryId=[^[:space:]]+: /,""); q=$0 } /s3:\/\//{ print "," $10 }' OFS=',' hive-server2.log)

echo $s3 | sed '$!d'

It will simply print:-

2018-02-21T18:05:34

Hope this will help you.

查看更多
登录 后发表回答