按字符串打印文本

按字符串打印文本

我在 cassandra-cli 的文本文件中得到了这个输出。我需要打印,RowKey并且OnlineUsers我正在使用巴什,并且grep不起作用。

Column Family assumptions read from /root/.cassandra/assumptions.json
Welcome to Cassandra CLI version 1.2.19

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] use Agent;
Authenticated to keyspace: Agent
[default@Agent] list VCCs;
Using default limit of 100
Using default cell limit of 100
-------------------
RowKey: cienciaactiva
=> (name=AllegroIntegration, value={"enabled":true}, timestamp=1504725823694867)
=> (name=CALL_licensed, value=1, timestamp=1504725823700706)
=> (name=CHAT_licensed, value=1, timestamp=1504725823695468)
=> (name=Id, value=cienciaactiva, timestamp=1504725823696915)
=> (name=IsSystemVCC, value=0, timestamp=1504725823699902)
=> (name=MAIL_licensed, value=1, timestamp=1504725823699520)
=> (name=OnlineUsers, value=1, timestamp=1504725823698413)
=> (name=WEBCONTACT_licensed, value=1, timestamp=1504725823697610)
-------------------
RowKey: atento
=> (name=CHAT_licensed, value=0, timestamp=1459855264796678)
=> (name=Id, value=atentoperusedapal, timestamp=1459855264801483)
=> (name=IsSystemVCC, value=1, timestamp=1459855264802006)
=> (name=MAIL_licensed, value=0, timestamp=1459855264797787)
=> (name=OnlineUsers, value=5, timestamp=1459855264796155)
=> (name=WEBCONTACT_licensed, value=0, timestamp=1459855264803923)
-------------------
RowKey: tsc
=> (name=CHAT_licensed, value=0, timestamp=1464217813009934)
=> (name=Id, value=tsc, timestamp=1464217813012789)
=> (name=IsSystemVCC, value=1, timestamp=1464217813016091)
=> (name=MAIL_licensed, value=1, timestamp=1464217813010988)
=> (name=OnlineUsers, value=7, timestamp=1485884014708000)
=> (name=WEBCONTACT_licensed, value=0, timestamp=1464217813012116)

3 Rows Returned.
Elapsed time: 178 msec(s).
[default@Agent]

预期的输出类似于:

RowKey: cienciaactiva
name=OnlineUsers, value=1
-------------------
RowKey: atento
name=OnlineUsers, value=5
-------------------
RowKey: tsc
name=OnlineUsers, value=7

答案1

awk命令:

awk '/RowKey/; /name=OnlineUsers/{ 
        print substr($2, 2), substr($3, 1, length($3)-1); f=1 
     }f && /---/' file

输出:

RowKey: cienciaactiva
name=OnlineUsers, value=1
-------------------
RowKey: atento
name=OnlineUsers, value=5
-------------------
RowKey: tsc
name=OnlineUsers, value=7

答案2

grep应该适合这个目的:

$ grep -e '^RowKey:' -e name=OnlineUsers -e '^----' input
-------------------
RowKey: cienciaactiva
=> (name=OnlineUsers, value=1, timestamp=1504725823698413)
-------------------
RowKey: atento
=> (name=OnlineUsers, value=5, timestamp=1459855264796155)
-------------------
RowKey: tsc
=> (name=OnlineUsers, value=7, timestamp=1485884014708000)

如果您需要从 中删除时间戳OnlineUsers,您可以使用 GNU 轻松完成此操作awk

$ awk -F '[(,]' '/^RowKey:/ || /^----/ { print } /name=OnlineUsers/ { print $2 "," $3 }' input
-------------------
RowKey: cienciaactiva
name=OnlineUsers, value=1
-------------------
RowKey: atento
name=OnlineUsers, value=5
-------------------
RowKey: tsc
name=OnlineUsers, value=7

相关内容