Linux下的sed 指令常用語法

$ sed -i 's/<?/<?php/g' test.php
# 參數 -i :直接修改檔案,而不須標準輸出,將 test.php 內的 <? 全部置換為 <?php,g代表全部行,s代表置換

$ sed 's/^192.168./&112.1/' test.php
# 開頭行192.168的行,會在該行最後面增加 112.1

$ sed -i '13,$d' test.php
#刪除  檔案 test.php 第13行開始到最後一行,並直接修改檔案

$ sed '/require_once(/d' test.php
#刪除包含 require_once的所有行,並印出標準輸出

$ sed -n '/$var_config/w test2.php' test.php
# 把有$var_config字串的行寫入 test2.php

$ sed -i '/^$/d test.php
# 刪除所有空白行

$ sed -i 's?/n?/r/n?g' test2.php
# 把換行符號換成Windows環境下,為了辨別區隔符號,可以在s後接上?當區隔符號
 
各系統文件行換行符號
Mac: /r
Windows: /r/n
Linux/Unix: /n
 

參考資料
https://www.gnu.org/software/sed/manual/sed.html

這個網誌中的熱門文章

Upgrade php and httpd(apache) on CentOS 7

Installing VMware workstation pro on Ubuntu 22.X (Jammy Jellyfish)