📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| (同じ利用者による、間の3版が非表示) | |||
| 45行目: | 45行目: | ||
vi ~/.profile | vi ~/.profile | ||
<br> | <br> | ||
<syntaxhighlight lang="sh"> | |||
# ~/.profileファイル | # ~/.profileファイル | ||
export PATH="/<sedのインストールディレクトリ>/bin:$PATH" | export PATH="/<sedのインストールディレクトリ>/bin:$PATH" | ||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||
| 54行目: | 56行目: | ||
<br> | <br> | ||
<center> | <center> | ||
{| class="wikitable" | {| class="wikitable" | style="background-color:#fefefe;" | ||
|- | |- | ||
! オプション ! | ! style="text-align: center;background-color:#66CCFF;" | オプション | ||
! style="text-align: center;background-color:#66CCFF;" | 説明 | |||
|- | |- | ||
| -e <スクリプト名> || 指定したスクリプト(条件式)で変換処理を行う。 | | -e <スクリプト名> || 指定したスクリプト(条件式)で変換処理を行う。 | ||
| 83行目: | 86行目: | ||
== sedコマンドの条件式 == | == sedコマンドの条件式 == | ||
<center> | <center> | ||
{| class="wikitable" | {| class="wikitable" | style="background-color:#fefefe;" | ||
|- | |- | ||
! 条件式 ! | ! style="text-align: center;background-color:#66CCFF;" | 条件式 | ||
! style="text-align: center;background-color:#66CCFF;" | 説明 | |||
|- | |- | ||
| 行数 || 処理する行数を指定する | | 行数 || 処理する行数を指定する | ||
| 144行目: | 148行目: | ||
|} | |} | ||
</center> | </center> | ||
<br><br> | |||
== デリミタ == | |||
sedコマンドには、置換のデリミタ (区切り文字) が複数存在する。<br> | |||
機能的には同じ結果をもたらす。<br> | |||
<br> | |||
デリミタとしては他にも <code>:</code>、<code>@</code>、<code>#</code>などの文字を使用することができる。<br> | |||
<br> | |||
# <code>/</code>を区切り文字として扱う場合 | |||
sed -i "s/<lastmod>.*</lastmod>/<lastmod>${current_date}</lastmod>/g" hoge.xml | |||
# <code>|</code>を区切り文字として扱う場合 | |||
sed -i "s|<lastmod>.*</lastmod>|<lastmod>${current_date}</lastmod>|g" hoge.xml | |||
<br> | |||
これらの違いが重要になる理由を以下に示す。<br> | |||
* 可読性 | |||
*: パスやURLを含む文字列を扱う場合、/を含むことが多いため、|を使用すると読みやすくなることがある。 | |||
* エスケープの必要性 | |||
*: /をデリミタとして使用する場合、置換対象やパターン内に/が含まれている時、それをエスケープする必要がある。 | |||
*: |を使用すると、/をエスケープする必要がない。 | |||
<br> | |||
上記では、XMLタグを扱っているため、どちらを使用しても問題ない。<br> | |||
しかし、より複雑なパターンや<u>/ (スラッシュ)</u> を含む文字列を扱う場合は、<u>| (バーティカルバー)</u> を使用する方が便利な場合がある。<br> | |||
<br><br> | <br><br> | ||