我有類似下面的行。
[1] 07:37:38 [bb..], qw, Exited with errojjjvice not known
[2] 07:37:39 [bfb.], fg, , This is FILE1, Stderrle or directory
[3] 07:37:39 [SU..], b b, ,, Stderr: bash: /sauch file or directory
cat: /root/file1: No, such, file or directory
[4] 07:37:39 [SUCCESS], gh, , This is F :, No, such file or directory
[5] 07:37:39 [SUCCESS], jk, ,, Stderr: bash: , No, such file or directory
nnnnnn, oot/file1: No, such, file or directory
hylll;; ooppgggh
[6] 07:37:39 [SUCCESS], jjj, ,, Stderr: bash: No, such file or directory
cat: /root/file1: No, such, file or directory
需要將該行附加到其原始 cmd 輸出行,後面跟著逗號。並且應該以包含數字的方括號開頭,如下所示
[1] 07:37:38 [bb..], qw, Exited with errojjjvice not known
[2] 07:37:39 [bfb.], fg, , This is FILE1, Stderrle or directory
[3] 07:37:39 [SU..], b b, ,, Stderr: bash: /sauch file or directory, cat: /root/file1: No, such, file or directory
[4] 07:37:39 [SUCCESS], gh, , This is F :, No, such file or directory
[5] 07:37:39 [SUCCESS], jk, ,, Stderr: bash: , No, such file or directory, nnnnnn, oot/file1: No, such, file or directory, hylll;; ooppgggh
[6] 07:37:39 [SUCCESS], jjj, ,, Stderr: bash: No, such file or directory, cat: /root/file1: No, such, file or directory
答案1
如果很好理解,您想要連接不以方括號開頭的行。
這是一個 perl 方式:
perl -0777 -i -ape 's/\R(?=[^[])/, /g' file
解釋:
\R # any kind of linebreak (i.e. \r, \n, \r\n)
(?= # positive lookahead, zero-length assertion that make sure we have after:
[^[] # any character that is not an opening square bracket
) # end lookahead
替代品:
, # a comma followed by a space
給定範例的結果:
[1] 07:37:38 [bb..], qw, Exited with errojjjvice not known
[2] 07:37:39 [bfb.], fg, , This is FILE1, Stderrle or directory
[3] 07:37:39 [SU..], b b, ,, Stderr: bash: /sauch file or directory, cat: /root/file1: No, such, file or directory
[4] 07:37:39 [SUCCESS], gh, , This is F :, No, such file or directory
[5] 07:37:39 [SUCCESS], jk, ,, Stderr: bash: , No, such file or directory, nnnnnn, oot/file1: No, such, file or directory, hylll;; ooppgggh
[6] 07:37:39 [SUCCESS], jjj, ,, Stderr: bash: No, such file or directory, cat: /root/file1: No, such, file or directory