2017-06-30 15 views
0

grepコマンドに1つの問題があります。grepコマンドの出力を元に戻す方法は?

"ServicePassword":fooooooooooo 
    "ServiceUserName":barrrrrrrrr 
grep -E '"ServiceUserName"|"ServicePassword"' >> user.txt 

が、USER.TXTファイルで、私は「ServicePassword」最初のでしょう:私と呼ばれるスクリプトで私が使用foooooooooooその後、「ServiceUserName」:barrrrrrrrr はどのように私はそれを逆にgrepコマンドを使用することができ、最初ServiceUserNameを取得しますServicePasswordはuser.txtファイルに出力されますか?私はこのシーケンスだけが必要です。私は変更しようとします:

grep -E '""ServicePassword""|"ServiceUserName"' >> user.txt 

と何か....多分もっと良い解決策がありますか? (ソートリバース)を助けることができるかもしれない|sortまたは|sort -rを追加するだけで2つの項目がありますように、別のツールを使用しなければならない順序を変更するには、print lines matching a pattern

答えて

2

はgrepだけで、man grepをフィルタリングします。

grep -E '"ServiceUserName"|"ServicePassword"' | sort -r >> user.txt 
0

使用awk

awk '/ServicePassword/{pwd=$0};/ServiceUserName/{user=$0}; 
    END{printf pwd; print user}' 
2

あなたが使用してtac command下に適応することができます:

$ cat test.txt 
"ServicePassword":fooooooooooo 
"ServiceUserName":barrrrrrrrr 

$ egrep '"ServicePassword"|"ServiceUserName"' test.txt | tac 
"ServiceUserName":barrrrrrrrr 
"ServicePassword":fooooooooooo 
+0

'猫のtest.txt'で起動する必要はありません - ちょうど' egrepの「"ServicePasswordを"|" ServiceUserName "'test.txt | tac> user.txt' – RomanPerekhrest

関連する問題