-1

複数の html ファイルがあり、スペースを置き換えて、文字列内の文字列を小文字にする必要があります。(すべてLinuxで)

Exaple:
<html> ....
<a href="bla.com/CCC C C">ddd ddd ddd</a>
<a href="bla.com/CCC C">ddd ddd ddd</a>
...
</html>

Should result in:
<html> ....
<a href="bla.com/ccc_c_c">ddd ddd ddd</a>
<a href="bla.com/ccc_c">ddd ddd ddd</a>
...
</html>

そのようなページには他のリンクがありますが、それらは bla.com ではありません。通常の exp が使用されている場合 (bla.com が必要です)。CCC 部分は静的ではなく、任意の単語にすることができます!

それができるワンライナーはありますか?

4

1 に答える 1

1

スペースを置き換え、文字列内の文字列を小文字にする必要があります

単一の空白区切りのみの場合、次の 1 つのライナーでそれが行われます。

sed -E 's/(bla.com\/)(\w*)\s*(.*?")/\1\L\2_\L\3/g' file

$ echo '<a href="bla.com/CCC C">ddd ddd ddd</a>' | sed -E 's/(bla.com\/)(\w*)\s*(.*?")/\1\L\2_\L\3/g'
<a href="bla.com/ccc_c">ddd ddd ddd</a>

説明:

s/            # Substitution
(bla.com\/)   # Match the domain (captured)
(\w*)         # Match the following word (captured) 
\s*           # Followed by whitespace
(.*?")        # Capture everything left upto the closing "
/             # Replace with 
\1            # The captured domain
\L\2          # Lowercase first captured word
_             # Replace the whitespace with an underscore 
\L\3          # Lowercase rest of the match
/g            # Global

あなたの例のように複数のスペースが存在する可能性がある場合、私はワンライナーを考え出すことに困惑しています。

于 2012-12-04T18:22:29.020 に答える