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?

More than 1 year has passed since last update.

sedコマンドを触ってみた

Last updated at Posted at 2021-06-26

###sedとは
「Stream EDitor」の略
下記サイトによると
https://www.ibm.com/docs/ja/aix/7.2?topic=s-sed-command

sed コマンドは、指定された File パラメーターの行を編集スクリプトに従って変更し、それらを標準出力に書き出します。 sed コマンドには、変更する行を選択するための機能、および選択した行に対してのみ変更を行うための機能などが多数含まれています。

ちなみに、日本語で「セド」と読む。

###触ってみる
####基本構文:置換(sとg)

sed 's/置換前文字列/置換後文字列/g' ファイルのPATH

orgin.txtというファイルを用意した。中身は↓通り

~/Documents/sed » ls                                                                                                                               
origin.txt

~/Documents/sed » cat origin.txt                                                                                                                                                   
Daffodils
I wander'd lonely as a cloud
That floats on high o'er vales
and hills,
When all at once I saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

下のコマンドでorigin.txtの内容にある「I」を全て「You」に変更してみよう。

sed 's/I/You/g' ./origin.txt

全置換で出力された

~/Documents/sed » sed 's/I/You/g' ./origin.txt                                                                                                     
Daffodils
You wander'd lonely as a cloud
That floats on high o'er vales
and hills,
When all at once You saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

:red_circle: 注意点:出力として原文の内容を置換したものが表示されるだけで、origin.txtの内容は原文のまま。↓のように新規ファイルに保存することでファイルとして出力もできる。

~/Documents/sed » sed 's/I/You/g' ./origin.txt > target.txt  
                                                                                                                      
~/Documents/sed » cat target.txt                                                                                                                    
Daffodils
You wander'd lonely as a cloud
That floats on high o'er vales
and hills,
When all at once You saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

sとgについて
📌最初のs:置換処理(substitute)
📌最後のg:マッチした文字列をすべて置換(global)

:notebook_with_decorative_cover: sの前に数字を入れると、指定した行目だけが対処になる
試しに2行目の「I」だけを「You」に置換してみる。

~/Documents/sed » sed '2s/I/You/g' ./origin.txt                                                                                                                              
Daffodils
*You* wander'd lonely as a cloud
That floats on high o'er vales
and hills,
When all at once *I* saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

できた。

2から5行目までに限定したい時に、下のように2,5で書けばいける

sed '2,5s/I/You/g' ./origin.txt 

####基本構文:削除(d)
内容の1行目から3行目を削除して4行目以降を出力するには、dで指定する

$ sed 1,3d ファイルのPath

実際やってみる

~/Documents/sed » cat origin.txt                                                                                                                                                   
Daffodils
I wander'd lonely as a cloud
That floats on high o'er vales
and hills,
When all at once I saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

~/Documents/sed » sed 1,3d ./origin.txt                                                                                                                                            
and hills,
When all at once I saw a crowd,
A host of golden daffodils,
Beside the lake, beneath the trees
Fluttering and dancing
in the breeze.

予想通りの結果となった。

以上

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?