how to select the last line of the shell output

后端 未结 3 402
甜味超标
甜味超标 2021-01-26 03:34

Hi I have a shell command like this.

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


        
相关标签:
3条回答
  • 2021-01-26 04:14

    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.

    0 讨论(0)
  • 2021-01-26 04:16

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

    tac file | awk '/match/{print; exit}'
    
    0 讨论(0)
  • 2021-01-26 04:36

    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.

    0 讨论(0)
提交回复
热议问题