
ここで見つけた同様の質問に対する追加の助けが必要です。複数行を単一行に変換
現在のSEDの例
(sed -e'y/)},={/(((((/' \
-e's/-\([^(I]*\)[^0-9]*\([0-9]*\)[( ]*/- \2 -\1/;=' |
paste -d- - - |
sort -t- -nk3,3 -nk1,1 |
sed -e's/^[^-]*-//;:n' -e'h;$!N' \
-e's/\(-\([^-]*-\).*[^ ]\) *\n\([^-]*-\)\{2\}\2/\1 - \3/;tn' \
-ex\;:t -e's/\(\([^-]*-\)[^/]*\) - *\2/\1,/;tt' -e'p;g;D' ) \
< in.txt > out.txt
複数行のログに従う
6/13/2015 12:00:47 AM - { 562} START Web
6/13/2015 12:00:47 AM - Requested Web connection from 123.125.71.103[123.125.71.103], ID=562
6/13/2015 12:01:24 AM - { 563} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=563
6/13/2015 12:01:24 AM - ( 563) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=563
6/13/2015 12:01:24 AM - { 563} END POP3
6/13/2015 12:01:24 AM - { 564} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=564
6/13/2015 12:01:24 AM - ( 564) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=564
6/13/2015 12:01:24 AM - { 564} END POP3
6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ID=562
6/13/2015 12:01:40 AM - { 562} END Web
に変換する
6/13/2015 12:00:47 AM - 562 - START Web, Requested Web connection from 123.125.71.103 [123.125.71.103] - 6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended., END Web
6/13/2015 12:01:24 AM - 563 - START POP3, Requested POP3 connection from 10.127.251.37 [10.127.251.37], USER [email protected], POP3 connection with 10.127.251.37 [10.127.251.37] ended., END POP3
6/13/2015 12:01:24 AM - 564 - START POP3, Requested POP3 connection from 10.127.251.37 [10.127.251.37], USER [email protected], POP3 connection with 10.127.251.37 [10.127.251.37] ended., END POP3
角かっこ内に次のIDを含むログを含めるようにSEDを変更したい(例[ 667]
:)。
ログの例
6/13/2015 2:15:09 AM - Starting delivery thread, ID=667
6/13/2015 2:15:09 AM - { 667} START Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 start): 0
6/13/2015 2:15:09 AM - [ 667] Delivering to [email protected]
6/13/2015 2:15:09 AM - [ 667] Forwarding mail...
6/13/2015 2:15:09 AM - [ 667] Forwarding message to 1 address(es)
6/13/2015 2:15:09 AM - Delivery thread, ID=667, terminated after running for 0.031 seconds.
6/13/2015 2:15:09 AM - { 667} END Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 end): 0
2番目の質問は、IDのないログを無視する方法です。それほど重要ではありませんが、フィルタリングすることをお勧めします。
たとえば、
6/13/2015 2:43:24 AM - Exended logging
6/13/2015 2:43:24 AM - Setting msgSizeLimit 15
よろしくお願いします。
答え1
sed
これは、ここにリストされているすべての可能性を処理するために修正する必要がある最初のことです。特に、単一の正規表現ですべての可能性を処理するのではなく、2つの異なる種類の行を別々に処理する必要があります。ID
-
行の最初のダッシュとその行の別の種類のダッシュの直後には、ダッシュマークがあります。ID=
他の場所。
( sed -e= -e's/- [({[] *\([0-9]*\) *[])}]/- \1 -/;t' \
-e's/-\(.*[^, ]\)[, ]*ID=\([0-9]*\)[, ]\{0,1\}/- \2 -\1/' |
paste -d- - - |
sort -t- -nk3,3 -nk1,1 |
sed -e's/^[^-]*-//;:n' -e'h;$!N' \
-e's/\(-\([^-]*-\).*[^ ]\) *\n\([^-]*-\)\{2\}\2/\1 - \3/;tn' \
-ex\;:t -e's/\(\([^-]*-\)[^/]*\)- *\2/\1:::/;tt' -e'p;g;D'
) <in >out
今回は2番目ですが、拡張-E
正規表現構文を使用して作成されました。これは少なくともBSD / GNU / ASTで動作しますsed
:
( sed -Ee= -e's/- [({[] *([0-9]+) *[])}]/- \1 -/;t' \
-e's/-(.*[^, ])[, ]*ID=([0-9]+)[, ]?/- \2 -\1/' |
paste -d- - - |
sort -t- -nk3,3 -nk1,1 |
sed -Ee's/^[^-]*-//;:n' -e'h;$!N' \
-e's/(-([^-]+-).*[^ ]) *\n([^-]+-){2}\2/\1 - \3/;tn' \
-ex\;:t -e's/(([^-]+-)[^/]*)- *\2/\1:::/;tt' -e'p;g;D'
) <in >out
これで、すべてのログデータの接続をテストしました。
6/13/2015 12:00:47 AM - { 562} START Web
6/13/2015 12:00:47 AM - Requested Web connection from 123.125.71.103[123.125.71.103], ID=562
6/13/2015 12:01:24 AM - { 563} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=563
6/13/2015 12:01:24 AM - ( 563) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=563
6/13/2015 12:01:24 AM - { 563} END POP3
6/13/2015 12:01:24 AM - { 564} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=564
6/13/2015 12:01:24 AM - ( 564) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=564
6/13/2015 12:01:24 AM - { 564} END POP3
6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ID=562
6/13/2015 12:01:40 AM - { 562} END Web
6/13/2015 2:15:09 AM - Starting delivery thread, ID=667
6/13/2015 2:15:09 AM - { 667} START Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 start): 0
6/13/2015 2:15:09 AM - [ 667] Delivering to [email protected]
6/13/2015 2:15:09 AM - [ 667] Forwarding mail...
6/13/2015 2:15:09 AM - [ 667] Forwarding message to 1 address(es)
6/13/2015 2:15:09 AM - Delivery thread, ID=667, terminated after running for 0.031 seconds.
6/13/2015 2:15:09 AM - { 667} END Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 end): 0
私の結果は次のとおりです
6/13/2015 12:00:47 AM - 562 - START Web ::: Requested Web connection from 123.125.71.103[123.125.71.103] - 6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ::: END Web
6/13/2015 12:01:24 AM - 563 - START POP3 ::: Requested POP3 connection from 10.127.251.37 [10.127.251.37] ::: USER [email protected] ::: POP3 connection with 10.127.251.37 [10.127.251.37] ended. ::: END POP3
6/13/2015 12:01:24 AM - 564 - START POP3 ::: Requested POP3 connection from 10.127.251.37 [10.127.251.37] ::: USER [email protected] ::: POP3 connection with 10.127.251.37 [10.127.251.37] ended. ::: END POP3
6/13/2015 2:15:09 AM - 667 - Starting delivery thread ::: START Delivery ::: Active delivery threads (start): 0 ::: Delivering to [email protected] ::: Forwarding mail... ::: Forwarding message to 1 address(es) ::: Delivery thread terminated after running for 0.031 seconds. ::: END Delivery ::: Active delivery threads (end): 0