• 當前位置:首頁 > IT技術 > 系統服務 > 正文

    linux中sed的使用方法具體解釋(對行數據的加入、刪除等)
    2021-10-08 17:27:25


    sed使用語法
    [root@fwq test]# sed --help
    使用方法: sed [選項]... {腳本(假設沒有其它腳本)} [輸入文件]...

    ? -n, --quiet, --silent???????????????? 取消自己主動打印模式空間

    ? -e 腳本, --expression=腳本???????????????? 加入“腳本”到程序的執行列表

    ? -f 腳本文件, --file=腳本文件???????????????? 加入“腳本文件”到程序的執行列表

    ? --follow-symlinks???????????????? follow symlinks when processing in place; hard links?will still be broken.

    ? -i[SUFFIX], --in-place[=SUFFIX]

    ???????????????? edit files in place (makes backup if extension supplied).

    ???????????????? The default operation mode is to break symbolic and hard links.

    ???????????????? This can be changed with --follow-symlinks and --copy.

    ? -c, --copy

    ???????????????? use copy instead of rename when shuffling files in -i mode.

    ???????????????? While this will avoid breaking links (symbolic or hard), the

    ???????????????? resulting editing operation is not atomic.? This is rarely

    ???????????????? the desired mode; --follow-symlinks is usually enough, and

    ???????????????? it is both faster and more secure.

    ? -l N, --line-length=N???????????????? 指定“l”命令的換行期望長度

    ? --posix???????????????? 關閉全部 GNU 擴展

    ? -r, --regexp-extended???????????????? 在腳本中使用擴展正則表達式

    ? -s, --separate???????????????? 將輸入文件視為各個獨立的文件而不是一個長的連續輸入

    ? -u, --unbuffered???????????????? 從輸入文件讀取最少的數據。更頻繁的刷新輸出

    ????? --help???? 打印幫助并退出

    ????? --version? 輸出版本號信息并退出

    ?

    ?

    打印出行號,并刪除2-5行

    [root@fwq test]# nl /etc/passwd | sed '2,5d' |more
    ?
    ??? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    ???? 6? sync:x:5:0:sync:/sbin:/bin/sync

    ???? 7? shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

    ???? 8? halt:x:7:0:halt:/sbin:/sbin/halt

    ???? 9? mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

    ??? 10? uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

    打印出行號,僅僅刪除2-5行

    [root@fwq test]# nl /etc/passwd | sed '2d' |more

    ???? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    ???? 3? daemon:x:2:2:daemon:/sbin:/sbin/nologin

    ???? 4? adm:x:3:4:adm:/var/adm:/sbin/nologin

    ???? 5? lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

    打印出行號,刪除第2行以后全部內容

    [root@fwq test]# nl /etc/passwd | sed '2,$d'

    ???? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    [root@fwq test]#


    打印出行號,在第2行后加上“drink tea”

    [root@fwq test]# nl /etc/passwd |sed '2a drink tea' | more

    ???? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    ???? 2? bin:x:1:1:bin:/bin:/sbin/nologin

    drink tea???? 3? daemon:x:2:2:daemon:/sbin:/sbin/nologin

    ???? 4? adm:x:3:4:adm:/var/adm:/sbin/nologin

    打印出行號。增加2行“drink tea or drink beer”

    [root@fwq test]# nl /etc/passwd |sed '2a drink tea or ...

    drink beer?

    ' | more

    ???? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    ???? 2? bin:x:1:1:bin:/bin:/sbin/nologin

    drink tea or ...

    drink beer?

    ???? 3? daemon:x:2:2:daemon:/sbin:/sbin/nologin

    ???? 4? adm:x:3:4:adm:/var/adm:/sbin/nologin

    打印出行號,將第2-5行的內容更換成“No 2-5”

    [root@fwq test]# nl /etc/passwd | sed '2,5c No 2-5 number' | more???? 1? root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

    No 2-5 number

    ???? 6? sync:x:5:0:sync:/sbin:/bin/sync

    ???? 7? shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

    ???? 8? halt:x:7:0:halt:/sbin:/sbin/halt

    打印出行號。僅列出第5-7行的內容

    [root@fwq test]# nl /etc/passwd | sed -n '5,7p'???? 5? lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

    ???? 6? sync:x:5:0:sync:/sbin:/bin/sync

    ???? 7? shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

    ?

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码