Shell – Get specific line from command output in awk

awkpipeshell

I have used pipeline to read specific record from impala shell. Here is what I got

[cloudera@localhost ~]$ echo "select * from abc where key > 'a-26052014015400' limit 1;" | impala-shell
Starting Impala Shell without Kerberos authentication
Connected to localhost.localdomain:21000
Server version: impalad version cdh5-1.3.0 RELEASE (build 40e1b62cf0b97f666d084d9509bf9639c575068c)
Welcome to the Impala shell. Press TAB twice to see a list of available commands.

Copyright (c) 2012 Cloudera, Inc. All rights reserved.

(Shell build version: Impala Shell vcdh5-1.3.0 (40e1b62) built on Tue Mar 25 13:46:44 PDT 2014)
Query: select * from abc where key > 'a-26052014015400' limit 1
[localhost.localdomain:21000] > +------------------------+------+----------------+-------+
| key                    | hpid | uts            | value |
+------------------------+------+----------------+-------+
| a-26052014015700 | HS2  | 26052014015450 | 50    |
+------------------------+------+----------------+-------+
Returned 1 row(s) in 2.42s
Goodbye

What I really want is a-26052014015700 | HS2 | 26052014015450 | 50 this record in awk programming. I have tried with pipelined awk command

`echo "select * from abc where key > 'a-26052014015400' limit 1;" | impala-shell| awk -F'=' '{print $2}' | awk -F '>' '{print $1}`

but didn't get the expected output. Any better and efficient method to extract the record?

Best Answer

try to pipe it to grep:

$  grep -E "| a-[0-9]* | HS2  | [0-9]* | [0-9]* |" 

to get rid of the first | and the last |:

$  grep -Eo "  a-[0-9]* \| HS2 \| [0-9]* \| [0-9]* " 

"-E" to access the extended regular expression syntax

"-o" is used to only output the matching segment of the line, rather than the full contents of the line.

Related Question