35 sedコマンド

6365 ワード

sedは、テキスト処理において非常に重要なツールであり、正規表現と完璧に組み合わせて使用でき、機能が異なります.処理時には、現在処理されている行を一時バッファに格納して「パターン空間」(pattern space)と呼び、次にバッファ内の内容をsedコマンドで処理し、処理が完了するとバッファの内容を画面に送る.次に、ファイルの最後まで次の行を処理します.リダイレクトストレージ出力を使用しない限り、ファイルの内容は変更されません.Sedは主に1つ以上のファイルを自動的に編集するために使用されます.ファイルの繰り返し操作を簡素化する.変換プログラムなどを作成します.
このテキストを使用してプレゼンテーションを行います.
$ cat pets.txt
This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam

置換
その中のmy文字列をyourに置き換えます.次の文はよく理解するはずです(sは置換コマンドを表し、/my/はマッチングmyを表し、/your/はマッチングをHao Chen’sに置き換え、/gは1行のすべてのマッチングを置き換えます).
root@ubuntu:~# sed "s/my/your/g" pets.txt 
This is your cat
  your cat's name is betty
This is your dog
  your dog's name is frank
This is your fish
  your fish's name is george
This is your goat
  your goat's name is adam


注意:上のsedはファイルの内容を変更していません.処理後の内容を出力するだけです.ファイルに書き込む場合は、リダイレクトを使用します.または-iパラメータを使用して、ファイルの内容を直接変更します.
root@ubuntu:~# 
root@ubuntu:~# cat pets.txt 
This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam
root@ubuntu:~# sed -i "s/my/your/g" pets.txt 
root@ubuntu:~# cat pets.txt 
This is your cat
  your cat's name is betty
This is your dog
  your dog's name is frank
This is your fish
  your fish's name is george
This is your goat
  your goat's name is adam


各行の前に何かを追加します.
sed "s/^/#/g" pets.txt
sed "s/^This/#This/g" pets.txt
sed "s/^This/#/g" pets.txt
root@ubuntu:~# sed "s/^/#/g" pets.txt
#This is your cat
#  your cat's name is betty
#This is your dog
#  your dog's name is frank
#This is your fish
#  your fish's name is george
#This is your goat
#  your goat's name is adam
root@ubuntu:~# sed "s/^This/#This/g" pets.txt
#This is your cat
  your cat's name is betty
#This is your dog
  your dog's name is frank
#This is your fish
  your fish's name is george
#This is your goat
  your goat's name is adam
root@ubuntu:~# sed "s/^This/#/g" pets.txt
# is your cat
  your cat's name is betty
# is your dog
  your dog's name is frank
# is your fish
  your fish's name is george
# is your goat
  your goat's name is adam

各行の後ろに何かを加える
root@ubuntu:~# sed "s/$/\!\!\!/g" pets.txt
This is your cat!!!
  your cat's name is betty!!!
This is your dog!!!
  your dog's name is frank!!!
This is your fish!!!
  your fish's name is george!!!
This is your goat!!!
  your goat's name is adam!!!

sedで使用可能な正規表現のアンカー文字:

  • abc>abcで終わる語
  • を表す
    例えば、あなたをherに変えたいと思っています.
    root@ubuntu:~# sed "s/\

    またherをhisに変えたい:
    root@ubuntu:~# sed "s/\ herpets.txt
    root@ubuntu:~# sed "s/er\>/is/g" herpets.txt 
    This is his cat
      his cat's name is betty
    This is his dog
      his dog's name is frank
    This is his fish
      his fish's name is george
    This is his goat
      his goat's name is adam
    

    もう一つの例
    root@ubuntu:~# cat html.txt 
    This is what I meant. Understand?
    

    htmlラベルを削除するには、次の正規表現を使用します.
    root@ubuntu:~# sed "s/<.>//g" html.txt 
     meant. Understand?
    

    得られた結果は間違っています.なぜですか.正規表現<.>は<>の中間の任意の文字を表し、任意の数の文字を表すからである.一致する結果は次のとおりです.
    This is what I
    

    次の式を使用して、正常に一致します.
    root@ubuntu:~#  sed 's/]*>//g' html.txt
    This is what I meant. Understand?
    

    かっこ内の内容は、>を除く文字が0回以上繰り返されることを意味します.
    sedで量詞を使う
    3行目のpatternのみ
    root@ubuntu:~# sed '3s/your/my/' pets.txt 
    This is your cat
      your cat's name is betty
    This is my dog
      your dog's name is frank
    This is your fish
      your fish's name is george
    This is your goat
      your goat's name is adam
    
    

    置換3,6行pattern
    root@ubuntu:~# sed '3,6s/your/my/' pets.txt 
    This is your cat
      your cat's name is betty
    This is my dog
      my dog's name is frank
    This is my fish
      my fish's name is george
    This is your goat
      your goat's name is adam
    

    各行の最初のsを置換
    root@ubuntu:~# sed 's/s/S/1' pets.txt 
    ThiS is your cat
      your cat'S name is betty
    ThiS is your dog
      your dog'S name is frank
    ThiS is your fish
      your fiSh's name is george
    ThiS is your goat
      your goat'S name is adam
    

    各行の2番目以降のsを置き換えます.
    root@ubuntu:~# sed 's/s/S/2g' pets.txt 
    This iS your cat
      your cat's name iS betty
    This iS your dog
      your dog's name iS frank
    This iS your fiSh
      your fish'S name iS george
    This iS your goat
      your goat's name iS adam
    

    複数照合
    一度に複数のパターンを一致させる必要がある場合は、次の方法を使用します.
    root@ubuntu:~# sed '1,3s/your/my/g; 3,$s/This/That/g' pets.txt 
    This is my cat
      my cat's name is betty
    That is my dog
      your dog's name is frank
    That is your fish
      your fish's name is george
    That is your goat
      your goat's name is adam
    
    

    一致する変数として&を使用できます.
    root@ubuntu:~# sed 's/your/[&]/g' pets.txt 
    This is [your] cat
      [your] cat's name is betty
    This is [your] dog
      [your] dog's name is frank
    This is [your] fish
      [your] fish's name is george
    This is [your] goat
      [your] goat's name is adam
    

    グループ化
    root@ubuntu:~# cat my.txt 
    This is your cat, your cat's name is betty
    This is your dog, your dog's name is frank
    That is your fish, your fish's name is george
    That is my goat, my goat's name is adam
    root@ubuntu:~# sed 's/This is your \([^,]*\), .* is \(.*\)/\1:\2/g' my.txt
    cat:betty
    dog:frank
    That is your fish, your fish's name is george
    That is my goat, my goat's name is adam