2016-04-18 2 views
2

この解決方法はスクリプトの一部になります。bashの行から2つの部分文字列を抽出して連結します。

エラーログがあれば、失敗したステータス/理由とユーザー名を抽出し、それらを連結して出力​​を電子メールで送信できるようにする必要があります。密接にこのようなエラー・ログ・ファイルに

Operation status: failed,Job Description not updated because this is not a matching Job Description ID.,username=FOO 
Operation status: failed,Job Description not updated because this is not a matching Job Description ID.,username=BAR 

各ライン:

私の出力は次のようになります

"{ Operation status: failed,Job Description not updated because this is not a matching Job Description ID.,Sent Data:{lastAppraisalScore=0.0, country=null, jobTitle=LABORER..., username=FOO},...sendAcctActNotif=N}}" 

何の楕円は、ファイル内にありません。各行にはかなり多くが含まれていますが、私は重要な部分だけを示しています。

+1

これについてあなたの研究努力を共有できますか? 'set -x'オプションでスクリプトをデバッグしようとしましたか? – Inian

+3

各行はJSONに有効ですか?もしそうなら、おそらく、各行を 'jq'に渡し、' Operation status'と 'username'キーを選択することでこれを達成できます。 –

+0

私は 'set -x'オプションでデバッグを試みていません。 'egrep -o '動作ステータス:" input "は必要なものを部分的に返しますが、最初の'。、'まで抽出してエラーコード全体を取得する必要があります。 – sharpmartin6

答えて

1

this answerを使用すると、ラインの一部を抽出することができます(常に同じように見える場合)。そして、あなたはラインで、あなたのファイルの行を処理することができます。

while read line; do 
    status=`grep -oP '(?<=Operation status:).*?(?=Sent)' <<< "$line"` 
    user=`grep -oP '(?<=username=).*?(?=})' <<< "$line" | head -n 1` 
    echo "Operation status:" $status "username="$user 
done < file.txt 

私の例file.txtなど:

{ Operation status: failed1,Job Description not updated because this is not a matching Job Description ID.,Sent Data:{lastAppraisalScore=0.0, country=null, jobTitle=LABORER..., username=FOO1}, {username=BAR1}...sendAcctActNotif=N}} 
{ Operation status: failed2,Job Description not updated because this is not a matching Job Description ID.,Sent Data:{lastAppraisalScore=0.0, country=null, jobTitle=LABORER..., username=FOO2}, {username=BAR2}...sendAcctActNotif=N}} 
{ Operation status: failed3,Job Description not updated because this is not a matching Job Description ID.,Sent Data:{lastAppraisalScore=0.0, country=null, jobTitle=LABORER..., username=FOO3}, {username=BAR3}...sendAcctActNotif=N}} 

マイ出力:

Operation status: failed1,Job Description not updated because this is not a matching Job Description ID., username=FOO1 
Operation status: failed2,Job Description not updated because this is not a matching Job Description ID., username=FOO2 
Operation status: failed3,Job Description not updated because this is not a matching Job Description ID., username=FOO3 
+0

これは動作します、ありがとうございます。しかし、私は、エラーログの各行に 'username ='の2つのインスタンスがあることに気が付いたので、両方とも返されます!どのようにして各行にユーザ名の最初のインスタンスだけを返すことができますか? – sharpmartin6

+1

@ sharpmartin6あなたが使用することができます|最初の要素(第1の線)を保持するためにヘッド-n 1を使用する。私の編集を見てください – Fabich

+0

素早い対応をありがとうございました。 '| head -n 1'はファイル内の最初の要素のみを返します。 'grep'を使って各行にusernameの最初のインスタンスだけを返すことは可能ですか? – sharpmartin6

2

シンプルsed置換がより適切なと思われます。

sed -n '/^"{ Operation status: failed,/!b 
    s///;s/,Sent.*, username=/\t/;s/}.*//p' file 

これは最初の式を検索します。見つからなければ、この行をバイパスします。それ以外の場合は、一致した文字列を何も置き換えずに、保持したくない他の文字列を置換し、最後に残っている文字列を印刷します。

+0

コードを実行しようとするとこのエラーが発生します。 'sed:\' s /// ''にジャンプするためのラベルが見つかりません。 – sharpmartin6

+0

'b'の後にセミコロンを追加してみてください。私はすべての 'sed'方言が文の区切り文字としてそこに改行を追加すると期待していたでしょうが、明らかにそうではありません。 – tripleee

+0

しかし、私は不足している '!'(not) – tripleee

関連する問題