3

1行の部分文字列を同じ長さのパターンに置き換えるSEDワンライナー(awkなし)の書き方について助けが必要です。たとえば、ファイルが次のようになっているとします。

デーモン www X=1 ***Y=1,2,2*** Z=
デーモン www X=1 ***Y=1,2,2,1,3,4,5*** Z=4
デーモンメール a=3

「daemon www」と部分文字列 Y=.. を含む行のみを変更したいと思います。ゼロの数は同じです。(枚数#,#,#,#は不明)。

出力ファイルは次のようになります。

デーモン www X=1 ***Y=0,0,0*** Z=4
デーモン www X=1 ***Y=0,0,0,0,0,0,0*** Z=4
デーモンメール a=3

何か案は ?ありがとう !

4

4 に答える 4

4

一方通行:

 sed '/daemon www.*Y=/{:l s/\(Y=\(0,\)*\)[0-9]*/\10/;/Y=\(0,\)*0\([^,0-9]\|$\)/!bl}' input

いくつかの説明:

if this line contains daemon www and Y=        #  /daemon www.*Y=/{
    loop                                       # :l
      a. find Y= and zeros followed by commas  # s/\(Y=\(0,\)*\)
      b. find a series of digits               # [0-9]*
      c. replace matches in 'a' and 'b' with   # /\10/g
         'a' and 0 
      d. jump to loop if cannot match the 
         desired pattern: Y=0,0..,0            # /Y=\(0,\)*0
      e. and the pattern in d ends with a 
         non-digit non-comma character or the 
         end of line                           # \([^,0-9]\|$\)/!bl
于 2013-06-16T22:03:47.563 に答える
2

試す:

sed '/^daemon www.*Y/{s/[0-9]\([,*]\)/0\1/g}'

実際に:

$ cat input
daemon www X=1 ***Y=1,2,2*** Z=
daemon www X=1 ***Y=1,2,2,1,3,4,5*** Z=4
daemon mail a=3

$ sed '/^daemon www.*Y/{s/[0-9]\([,*]\)/0\1/g}' input
daemon www X=1 ***Y=0,0,0*** Z=
daemon www X=1 ***Y=0,0,0,0,0,0,0*** Z=4
daemon mail a=3
于 2013-06-16T22:05:05.540 に答える
0

これを試して:

sed -e 's/^\(daemon www X=1 .*Y=\)[0-9]\+/\10/' -e 's/,[0-9]\+/,0/g'
于 2013-06-16T22:23:10.047 に答える