chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

??sed命令從入門到實戰(zhàn)

馬哥Linux運維 ? 來源:博客園 ? 2025-06-18 15:59 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

sed 命令詳解及示例

sed是一種流編輯器,能高效地完成各種替換、刪除、插入等操作,按照文件數(shù)據(jù)行順序,重復處理滿足條件的每一行數(shù)據(jù),然后把結果展示打印,且不會改變原文件內容。

sed會逐行掃描輸入的數(shù)據(jù),并將讀取的數(shù)據(jù)內容復制到臨時緩沖區(qū)中,稱為“模式空間”(pattern space),然后拿模式空間中的數(shù)據(jù)與給定的條件進行匹配,如果匹配成功,則執(zhí)行特定的sed指令,否則跳過輸入的數(shù)據(jù)行,繼續(xù)讀取后面的數(shù)據(jù)。

一、命令介紹

1.1 命令語法

|   |   |
| --- | --- |
|   | sed [OPTION]... {script-only-if-no-other-script} [input-file]... |
|   |   |
|   |#如: |
|   | sed [選項]'匹配條件和操作指令'文件名 |
|   |cat文件名 | sed [選項]'匹配條件和操作指令'|

1.2 選項參數(shù)

|   |   |
| --- | --- |
|   | -n, --quiet, --silent |
|   |#suppress automatic printing of pattern space |
|   | --debug  #annotate program execution |
|   | -e script, --expression=script |
|   |#add the script to the commands to be executed |
|   | -f script-file, --file=script-file |
|   |#add the contents of script-file to the commands to be executed |
|   | --follow-symlinks |
|   |#follow symlinks when processing in place |
|   | -i[SUFFIX], --in-place[=SUFFIX] |
|   |#edit files in place (makes backup if SUFFIX supplied) |
|   | -l N, --line-length=N |
|   |#specify the desired line-wrap length for the 'l' command |
|   | --posix    #disable all GNU extensions. |
|   | -E, -r, --regexp-extended |
|   |#use extended regular expressions in the script (for portability use POSIX -E). |
|   | -s, --separate |
|   |#consider files as separate rather than as a single, continuous long stream. |
|   | --sandbox |
|   |#operate in sandbox mode (disable e/r/w commands). |
|   | -u, --unbuffered |
|   |#load minimal amounts of data from the input files and flush the output buffers more often |
|   | -z, --null-data |
|   |#separate lines by NUL characters |
|   | --help  #display this help and exit |
|   | --version #output version information and exit |

選項 例子
-n, --quiet, --silent
禁止自動打印模式(常配合'p'使用,僅顯示處理后的結果)
sed -n '/hello/p' filename
使用 /hello/ 匹配含有 "hello" 的行,p 打印匹配的行
--debug
以注解的方式顯示 sed 的執(zhí)行過程,幫助調試腳本
sed --debug 's/foo/bar/' filename
當你使用 sed 修改內容時,它會顯示調試信息,以便你了解腳本是如何執(zhí)行的
-e script, --expression=script
在命令行中直接指定 sed 腳本(允許多個 sed 表達式)
sed -e 's/foo/bar/' -e 's/hello/world/' filename
將文件中的 foo 替換為 bar,然后將 hello 替換為 world
-f script-file, --file=script-file
從指定的腳本文件中讀取 sed 命令
sed -f script.sed filename
script.sed 是包含多個 sed 命令的腳本文件,sed 會按順序執(zhí)行這些命令
--follow-symlinks
當指定 -i 時,sed 會跟隨符號鏈接(symlink)指向的實際文件進行編輯
sed -i --follow-symlinks 's/foo/bar/' symlink.txt
如果 symlink.txt 是一個符號鏈接文件,sed 會編輯它指向的實際文件
-i[SUFFIX], --in-place[=SUFFIX]
直接編輯文件(如果提供 SUFFIX,則進行備份)
sed -i.bak 's/foo/bar/' filename
直接在 filename 中將 foo 替換為 bar,并創(chuàng)建一個備份文件 filename.bak
-l N, --line-length=N
當使用 l 命令(列出行內容)時,指定輸出的行寬(N 表示字符數(shù))
echo 'hello world' | sed -l 5 'l'
使用 l 命令顯示 "hello world",但每行最多顯示 5 個字符
--posix
禁用 GNU 擴展,使 sed 遵循 POSIX 標準語法
sed --posix 's/foo/bar/' filename
這將禁用 sed 的一些非標準特性,確保腳本在 POSIX 環(huán)境下工作
-E, -r, --regexp-extended
使用擴展的正則表達式(ERE),這與基本正則表達式(BRE)相比,簡化了一些語法(例如不用轉義括號和 +)
echo "abc123" | sed -E 's/[a-z]+([0-9]+)/1/'
使用擴展正則表達式,匹配并提取字母后面的數(shù)字
-s, --separate
將多個輸入文件視為獨立的流,而不是作為一個連續(xù)的流處理
sed -s 's/foo/bar/' file1.txt file2.txt
sed 會分別處理 file1.txt 和 file2.txt,而不是將它們作為一個整體處理
--sandbox
以沙盒模式運行,禁止使用 e, r, w 命令,防止 sed 修改文件或執(zhí)行外部命令
sed --sandbox 's/foo/bar/' filename
啟用沙盒模式,防止 sed 腳本執(zhí)行危險的操作
-u, --unbuffered
減少從輸入文件讀取數(shù)據(jù)時的緩沖區(qū)大小,并更頻繁地刷新輸出
sed -u 's/foo/bar/' filename
立即將處理結果輸出到標準輸出,而不是等到處理大量數(shù)據(jù)后再輸出
-z, --null-data
將輸入中的行分隔符從換行符 改為 NUL 字符 ?,這在處理二進制數(shù)據(jù)或以 NUL 作為分隔符的文本時很有用
sed -z 's/foo/bar/' filename
使用 NUL 字符作為行分隔符處理文本

1.3 匹配條件

格式 描述
/regexp/ 使用 "正則表達式",匹配數(shù)據(jù)行
n(數(shù)字) 使用 "行號" 匹配,范圍是1-$($ 表示最后一行)
addr1,addr2 使用 "行號或正則" 定位,匹配從 addr1 到 addr2 的所有行
addr1,+n 使用 "行號或正則" 定位,匹配從 addr1 開始及后面的 n 行
n~step 使用 "行號",匹配從行號 n 開始,步長為 step 的所有數(shù)據(jù)行

1.3.1 定界符

/ 在sed中作為定界符使用,也可以使用任意的定界符。

|   |   |
| --- | --- |
|   | sed's|foo|bar|g'filename |
|   | sed'sbar:g'filename |
|   |   |
|   |#定界符出現(xiàn)在樣式內部時,需要進行轉義: |
|   | sed's//bin//usr/bin/g'filename |

1.3.2 變量引用

sed表達式使用單引號來引用,但是如果表達式內部包含"變量"字符串,則需要使用雙引號。

|   |   |
| --- | --- |
|   | foo="world"|
|   |echo"hello world"| sed"s/$foo/librarookie"|
|   | hello librarookie |

1.4 操作指令

    指令 描述 例子
    !