9

MicrosoftWord文書から作成された巨大でひどい醜いHTMLファイルに対して実行したいsedコマンドがあります。文字列のインスタンスを削除するだけです

style='text-align:center; color:blue;
exampleStyle:exampleValue'

私が変更しようとしているsedコマンドは

sed "s/ style='[^']*'//" fileA > fileB

一致するテキスト内に新しい行がある場合は常に一致しないことを除いて、これはうまく機能します。sedの修飾子、または改行を含む任意の文字の一致を強制するためにできることはありますか?

正規表現はXMLとHTMLでひどいことを理解していますが、この場合、文字列パターンは、スタイル属性が常に一重引用符で始まり、一重引用符で終わるという点で整形式です。したがって、改行の問題を解決できれば、その1つのコマンドでHTMLのサイズを50%以上削減できます。


結局、SinanÜnürのperlスクリプトが最も効果的であることが判明しました。それはほぼ瞬時に行われ、ファイルサイズが2.3MBから850kに減少しました。古き良きPerl..。

4

6 に答える 6

4

Sed は入力を 1 行ずつ読み取るため、1 行に渡って処理を行うのは簡単ではありませんが、sed の分岐を利用する必要があるため、不可能ではありません。以下は機能します。何が起こっているのかを説明するためにコメントしました(最も読みやすい構文ではありません!):

sed "# if the line matches 'style='', then branch to label, 
     # otherwise process next line
     /style='/b style
     b
     # the line contains 'style', try to do a replace
     : style
     s/ style='[^']*'//
     # if the replace worked, then process next line
     t
     # otherwise append the next line to the pattern space and try again.
     N
     b style
 " fileA > fileB
于 2009-07-22T12:49:29.853 に答える
1

これを試すことができます:

awk '/style/&&/exampleValue/{
    gsub(/style.*exampleValue\047/,"")
}
/style/&&!/exampleValue/{     
    gsub(/style.* /,"")
    f=1        
}
f &&/exampleValue/{  
  gsub(/.*exampleValue\047 /,"")
  f=0
}
1
' file

出力:

# more file
this is a line
    style='text-align:center; color:blue; exampleStyle:exampleValue'
this is a line
blah
blah
style='text-align:center; color:blue;
exampleStyle:exampleValue' blah blah....

# ./test.sh
this is a line

this is a line
blah
blah
blah blah....
于 2009-07-22T12:54:19.433 に答える
1

別の方法は次のようなものです:

$ cat toreplace.txt 
I want to make \
this into one line

I also want to \
merge this line

$ sed -e 'N;N;s/\\\n//g;P;D;' toreplace.txt 

出力:

I want to make this into one line

I also want to merge this line

N別の行をロードPし、パターン スペースを最初の改行まで出力し、パターン スペースを最初の改行までD削除します。

于 2009-11-18T07:10:00.533 に答える