0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Linux][command] テキスト処理_置換・削除_tr, sed

Last updated at Posted at 2025-04-11

trコマンド

$ tr [オプション] [文字列1 [文字列2]]
オプション 由来 説明
default translate - [文字列1]を[文字列2]に置換
-d delete - [文字列1]でマッチした文字列を削除する
-s squeeze(絞る) - 連続するパターン文字列を1つに絞る
文字クラス 説明
[:alnum:] 英数字([:alpha:][:digit:] の組み合わせ)
[:alpha:] 英字(大文字・小文字)
[:digit:] 数字
[:lower:] 小文字の英字
[:upper:] 大文字の英字
[:space:] 空白文字全般(スペース、タブ、改行など)

sedコマンド

$ sed [オプション] 編集コマンド [ファイル名]

or

$ sed [オプション] -e 編集コマンド1 [-e 編集コマンド2...] [ファイル名]

or

$ sed [オプション] -f スクリプトファイル [ファイル名]
オプション 由来 説明
default stream editor - 入力ストリームに対して編集コマンドを実行(sed 's/置換前/置換後/'など)
-e expression - 複数の編集コマンドを順番に適用可能
-f file - 編集コマンドを記述したファイルを指定して実行
編集コマンド 由来 説明
y/文字1/文字2/ yank 文字の変換:axbycz に1文字ずつ変換
例:sed y/abc/xyz/ file.txt
s/文字列1/文字列2/ substitute 文字列の置換:各行の最初の foobar に置換
例:sed s/foo/bar/ file.txt
s/文字列1/文字列2/g substitute (global) すべての一致を置換:すべての foobar に置換
例:sed s/foo/bar/g file.txt
/文字列/d delete 一致した行を削除:foo にマッチした行を削除
例:sed /foo/d file.txt
d delete 指定行を削除:
2行目を削除
1から3行目を削除
例:sed 2d file.txt
例:sed 1,3d file.txt

Ping-t

tr

sed

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?