sed 應用筆記

2021-09-25 12:57:28 字數 1679 閱讀 2642

sed 的筆記

sed 的替換指令 有兩款。分別如下:

# 第一款

sed -i "s/$/$/g"

$# 第二款,帶有 $

# $ 可以出現 在 $ 的前面或者後面。

sed -i "/$/s/$/$/g"

$

假設/etc/sysctl.conf檔案上,有如下的 指令。

都替換為net.ipv4.tcp_sack = 0

net.ipv4.tcp_sack=1

net.ipv4.tcp_sack = 1

net.ipv4.tcp_sack = 1

net.ipv4.tcp_sack = ***x

net.ipv4.tcp_sack = 0

net.ipv4.tcp_sack = 0

net.ipv4.tcp_sack = 0

net.ipv4.tcp_sack = 0

可以使用以下兩款中的任意一款來進行替換。

# 第一款

sed -i "s/net.ipv4.tcp_sack\s*=\s*\s\+/net.ipv4.tcp_sack = 0/g"

/etc/sysctl.conf

# 第二款,帶有 $

# 可以寫出更簡短的 指令。

sed -i "/net.ipv4.tcp_sack/s/=\s*\s\+/= 0/g"

/etc/sysctl.conf

編寫了乙個指令碼。來方便以後使用:

function text_replace ()=

$1 local $=$2

local $=$3

local $=$4

if[ -z "$"];

then

# without regexp_previous

sed -i "s/$/$/g"

$else

# with regexp_previous

sed -i "/$/s/$/$/g"$fi

}

regexp 中,一般知道以下的 notation 就夠用了。

notation

description

.any char

\ddigit. 1, 2, 3, …, etc.

\dnot digit

\swhite space, tab, space, …, etc.

\snot white space

\walphabet or digit. 1, 2, 3, …, a, b, c, …, etc.

\wnot (alphabet or digit). +, -, *, /, …, white space, etc.

+repeat at least one

*repeat at least zero

^begin of line

$end of line

<

begin of word

>

end of word

tips:

使用 vim 來測試 regexp,是最直觀和快速的方法。

linux sed 命令

OpenCV應用筆記

1 使用cvsmooth函式,src與dst的depth要一致,否則會出現raiseexception錯誤。src和dst的位深不能為64位浮點。簡單模糊和高斯模糊支援 1 或 3 通道,8 位元 和 32 位元 浮點影象。這兩種方法可以 in place 方式處理影象。2 關於直接讀取iplima...

session應用筆記

session start 開始session會話處理 session只要用到這個,就必須開啟session start 放在檔案開頭 建立session,直接採用超級全域性變數賦值即可 session是存在伺服器端,一般存放1440秒,如果網頁沒有任何操作,會自動銷毀,當然,可以通過php.ini...

DPDK offload應用筆記

使用 ethtool 使用 ethtool 可以看一般的 網絡卡。ethtool k dpdk 的做法 在 dpdk 中可以使用以下函式來檢視是否支援 offload 功能。ixgbe dev info get struct rte eth dev dev,struct rte eth dev in...