目录

修改配置项的值

读取配置项的值

替换/修改

在匹配行行首/行尾添加字符(#)

去掉匹配行行首/行尾字符(#)

#匹配行前加

#匹配行后加

#匹配行前后加

删除匹配行

替换/删除匹配的字符之间的内容

按行读取配置获取参数执行命令|shell读取配置文件

删除空格

如何在sed中使用变量

第一

第二


修改配置项的值

multi_cluster_set_ha = yes 改为multi_cluster_set_ha = no

命令:

方法1:(这个需要参数名前面不能有空格)

sed -i "s/^参数名=.*/参数名=新值/" 配置文件路径

方法2:

把namelist.txt中user_name:后面的改成:Liudehua (注意:\1表示替换)

sed -r -i "s/(user_name:).*/\1Liudehua/" namelist.txt

把namelist.txt中user_name=后面的改成:Liudehua (注意:\1表示替换)

sed -r -i "s/(user_name=).*/\1Liudehua/" namelist.txt

从上面的例子可以知道(user_name=)表示匹配user_name=,然后.*表示任意个字符,
\1表示替换,连起来就是把匹配到的“user_name=”后面的任意个字符串替换成“Liudehua”

关于sed的sed元字符集的含义更多的见:https://www.cnblogs.com/maxincai/p/5146338.html

例子一:
要将vim /etc/onestor/onestor.conf中的

multi_cluster_set_ha = yes
handy_ha_needed = yes

改为:
multi_cluster_set_ha = no
handy_ha_needed = no

sed -i "s/^multi_cluster_set_ha.*\+=.*/multi_cluster_set_ha = no/"/etc/onestor/onestor.conf
sed -i "s/^handy_ha_needed.*\+=.*/handy_ha_needed = no/" /etc/onestor/onestor.conf

旧方案脚本:

sed -i "s/^multi_cluster_set_ha[[:space:]]\+=.*/multi_cluster_set_ha = no/" /etc/onestor/onestor.conf
sed -i "s/^handy_ha_needed[[:space:]]\+=.*/handy_ha_needed = no/" /etc/onestor/onestor.conf

因里面的[[:space:]] 代表匹配任意个空格(但不代表0个!!),遇到没有空格的就失败,所以用上面的替换,“.*”代表“”内任意个字符。

详细说明见文章末尾:
原文链接:https://blog.csdn.net/bandaoyu/article/details/120047612

读取配置项的值

获取“public_addr=172.17.31.109”等号左边的名字public_addr

item=`cat /etc/ceph/ceph.conf |grep -E 'public_addr'`&&echo ${item%%=*}

获取“public_addr=172.17.31.109”等号右边的值 72.17.31.109

line=`cat /etc/ceph/ceph.conf |grep -E 'public_addr'`&&echo ${line#*=}

替换/修改

PermitRootLogin no 替换成PermitRootLogin yes

sudo sed -i "s/PermitRootLogin no/PermitRootLogin yes/g" /etc/ssh/sshd_config 

在匹配行行首/行尾添加字符(#)

在每一行的行首/尾添加字符串

sed  -i "s/.*/行首添加内容&行尾添加内容/" 文件名

在匹配行的行首/尾添加字符串

在包含“STATD#u PORT”的行开头添加“#”注释符号

sed -i  "/STATD_PORT/s/^/#/" /tmp/file

在与“callout”匹配的行的末尾添加“your text”

sed  -i "/callout/s/$/your text/" /tmp/file

在以“STATD”开头的行的 末尾添加“your text”

sed  -i "/^STATD/s/$/your text/" /tmp/file

几点说明:

1."^"代表行首,"$"代表行尾
原文链接:https://blog.csdn.net/bandaoyu/article/details/120047612

去掉匹配行行首/行尾字符(#)

去掉行首#,"^"代表行首

sed -i "/have a nice day/ s/^#//"  a.txt 

 # /have a nice day/  代表匹配,s/^abcdf//  代表行首的abcdf字符串替换为后面两斜杠里的内容,这里为空,即删除。 

去掉行尾#,"$"代表行尾

sed -i "/have a nice day/ s/$#//"    a.txt

#匹配行前加

我的记法是a = after ,i = in front 

在匹配“2222222222”前面添加“3333333333”,我的记法是a = after ,i = in front 

sed -i "/2222222222/i\3333333333" test.txt

#匹配行后加

我的记法是a = after ,i = in front 

在匹配“2222222222”后面添加“3333333333”,我的记法是a = after ,i = in front 

sed -i "/2222222222/a\3333333333" test.txt

#匹配行前后加

加反斜杠只是为了容易区分,可以不用:

sed -i "/allow 361way.com/iallow www.361way.com" the.conf.file

sed -i "/allow 361way.com/aallow www.361way.com"  the.conf.file

这就就可以很方便的看出要在某一行前或某一行后加入什么内容 。

删除匹配行

删除匹配到preSql的行

sed -i "/preSql/da.txt

Linux命令之sed-删除匹配行 :https://www.cnblogs.com/yunjiaofeifei/p/14912272.html

替换/删除匹配的字符之间的内容

#删除<\/schema>和<heartbeat>字符串之间的内容


sed -i "/<\/schema>/,/<heartbeat>/{//!d}" ./schema.xml

#替换dataNode=".*".*rule="sharding-by-intfile" 为dataNode=" " rule="sharding-by-intfile"  .*表示0各或任意个字符

#sed用单引号,所以里面的双引号不用转移符号
sed -i 's/dataNode=".*".*rule="sharding-by-intfile"/dataNode=" " rule="sharding-by-intfile"/g' ./schema.xml

原文链接:https://blog.csdn.net/bandaoyu/article/details/120047612

# Delete text between patterns, excluding the lines containing these patterns(不删除所在行):

sed -i "/PATTERN-1/,/PATTERN-2/{//!d}" input.txt

# Delete text between patterns, including the lines containing these patterns(所在行也删除):

sed -i "/PATTERN-1/,/PATTERN-2/d" input.txt

# To delete all the lines after PATTERN, use this

sed -i  "/PATTERN/,$d" input.txt

[sed] Delete the lines lying in between two patterns | *NIX Tricks

sed -i "/#hwBegin/,/#hwEnd/d" /etc/my.cnf     #就是将#hwBegin行到#hwEnd行之间内容,包括

#hwBegin行和#hwEnd行都删除,-i表示编辑的是文件

#add '-----here is the content---' to CONF_DIR behind the "[mysql]"

#use command 'sed' to add A behind "pattern" :sed 's/pattern/&A/' filename

用命令'sed'在filename文件内匹配的字符"pattern"后面添加内容A

#!/bin/ash

CONF_DIR="/etc/my.cnf"

sed -i 's/\[mysqld\]/&\n \

wait_timeout=2073600\n \

interactive_timeout=2073600\n \

bulk_insert_buffer_size=16M\n \

max_allowed_packet=16M\n/'  /etc/my.cnf

sed命令删除特定行号

删除第N行

1

sed -i 'Nd' filename

删除第N~M行

1

sed -i 'N,Md' filename   # file的[N,M]行都被删除

删除shell变量表示的行号(配合for等语句使用)

1

sed -i "${var1},${var2}d" filename # 这里引号必须为双引号

删除最后一行

1

sed -i '$d' filename

sed命令删除包含特定字符行

删除包含"xxx"的行

1

sed -i '/xxx/d' filename

1、^代表行首
2、$代表行尾
3、所有行首增加sed -i 's/^/ABC/' a.txt
4、所有行尾添加sed -i 's/$/XYZ/' a.txt
5、删除首行sed -i '1d' d.txt
6、删除末行sed -i '$d' d.txt
7、第5行添加sed -i '5 r 5.txt' a.txt
8、删除空行sed -i '/^$/d' a.txt
9、剔除空格sed -i 's/[ ]*//g' ~/vip1.txt
10、删除回车符sed -i 's/^M//g' a.txt
11、从fromstart这行下面添加内容sed -i '/fromstart/r 4.txt' 5.txt
12、第一列排序存文件awk '{print $1}' vip1.txt |sort -n > vip2.txt

更多实例:

将配置文件中的ms_cluster_type = async+posix改为ms_cluster_type = async+rdma 或者反过来:

方法1:匹配修改

方法2:删除原配置行,添加新配置行

#ps -ef | grep fio | grep -v grep | awk '{print $2}' | xargs kill -s 9

CONF_PATH='/etc/ceph/ceph.conf'

if [ -z $1 ] ; then

echo "******************"
echo "Please input arg:"
echo "******************"
echo "-----------------------------------"
echo "p or P:change ceph conf to posix"
echo "r or R:change ceph conf to rdma"
echo "-----------------------------------"
exit 

else

TYPE=$1

fi


case ${TYPE} in
p|P) 


sed -i 's/^ms_cluster_type =.*/ms_cluster_type = async+posix/' ${CONF_PATH}
cat ${CONF_PATH} | grep 'ms_cluster_type'

if false; then
sed -i '/ms_cluster_type = async+posix/d' ${CONF_PATH}  #delete line 
sed -i '/ms_cluster_type = async+rdma/d'  ${CONF_PATH}  #delete line

sed -i '/ms_public_type = async+posix/a\ms_cluster_type = async+posix' ${CONF_PATH} #add line after 'ms_public_type = async+posix'
sed -i '/ms_public_type = async+posix/a\#ms_cluster_type = async+rdma' ${CONF_PATH} #add line after 'ms_public_type = async+posix'
fi

;;
r|R)


sed -i 's/^ms_cluster_type =.*/ms_cluster_type = async+rdma/' ${CONF_PATH}
cat ${CONF_PATH} | grep 'ms_cluster_type'


if false; then
sed -i '/ms_cluster_type = async+posix/d' ${CONF_PATH}  #delete line 
sed -i '/ms_cluster_type = async+rdma/d'  ${CONF_PATH}  #delete line

sed -i '/ms_public_type = async+posix/a\#ms_cluster_type = async+posix' ${CONF_PATH} #add line after 'ms_public_type = async+posix'
sed -i '/ms_public_type = async+posix/a\ms_cluster_type = async+rdma' ${CONF_PATH} #add line after 'ms_public_type = async+posix'
fi
;;
*)
esac
 

匹配任意个空格

[[:space:]]                         字符串内的任意个空格

\*[[:space:]]\+hard            行首到hard之间的任意个空格

hard[[:space:]]\+nofile      字符串hard到字符串nofile之间的任意个空格

[[:space:]] ,如果前面没有其他字符则 \*[[:space:]],[[:space:]]\+xxx表示xxx前面的任意个空格,所以xxx在行首,则\*[[:space:]]\+ 匹配行首的任意个空格

原文链接:https://blog.csdn.net/bandaoyu/article/details/120047612


场景:

*   soft      nofile 630000
*    hard   nofile 654300

grep 匹配如上文本,由于最后一列数字是可变的,并且每列之间可能存在任意个空格或制表符

grep   -E  '^\*[[:space:]]+soft[[:space:]]+nofile|^\*[[:space:]]+hard[[:space:]]+nofile'  /etc/security/limits.conf

将匹配的结果sed 替换最后一列数字

sed -n 's/\*[[:space:]]\+soft[[:space:]]\+nofile.*/\* soft nofile 654350/p;s/\*[[:space:]]\+hard[[:space:]]\+nofile.*/\* hard nofile 654350/p' /etc/security/limits.conf

区别,在匹配任意个数空格或制表符时,grep 的’+’ 不需要转义,sed 需要转义
补充:sed -r 可以实现正则表达式匹配

原文链接:https://blog.csdn.net/rockstics/article/details/111563857

按行读取配置获取参数执行命令|shell读取配置文件

 conf.txt

172.17.31.53:12345 1 1 10000 10 1024
172.17.31.53:12345 1 1 10000 10 2048
172.17.31.53:12345 1 1 10000 10 4096
172.17.31.53:12345 1 1 10000 10 8192

readconf.sh

xargs -I{} echo  {} < ./conf.txt

把echo换成自己的脚本/程序

方法二:

a=('1 2 3'  '4 5 6'  '7 8 9')
 
for i in "${a[@]}" ; do
b=($i) #此时b就相当于二维数组里面的一维数组了,然后可以再次遍历
    for j in "${b[@]}"; do
        #do someting
    done
done

shell练习--关于二维数组的实现问题 - 回眸一笑百贱生 - 博客园

shell脚本将文件按行读入二维数组

#!/bin/bash
unset array
for x in `cat xxx.properties`
{
    #字符串截取:从左至右第一个'='之前的内容
    #echo ${x%%=*}
    #字符串截取:从左至右第一个'='之后的内容
    #echo ${x#*=}
    array[${#array[@]}]="${x%%=*} ${x#*=}"
}

echo ${#array[@]}
echo ${array[@]}

删除空格

快速去除字符串中的空格:https://blog.csdn.net/kwame211/article/details/81231261

  • 删除行首空格

sed ‘s/^[ \t]*//g'

  • 删除行末空格

sed ‘s/[ \t]*$//g'

  • 删除所有的空格

sed s/[[:space:]]//g

1039  ceph -s
 1040  systemctl daemon-reload
 1041  ceph -s
 1042  systemctl start ceph-mds.target

restart ceph
sudo systemctl restart ceph.target


显示ceph
sudo ps -aux|grep -E 'ceph-/*|dse'|grep -v 'grep'|awk '{print $12,$13,$14,$15,$16,$17}'

显示配置

cat /etc/ceph/ceph.conf |grep -E 'ms_public_io_type|ms_public_type|ms_cluster_type'

 改配置项:
 改public配置为Posix
 sed -i 's/^ms_public_type[[:space:]]\+=.*/ms_public_type = async+posix/' /etc/ceph/ceph.conf

 改public配置为rdma
 sed -i 's/^ms_public_type[[:space:]]\+=.*/ms_public_type = async+rdma/' /etc/ceph/ceph.conf

改cluster配置为Posix
 sed -i 's/^ms_cluster_type[[:space:]]\+=.*/ms_cluster_type = async+posix/' /etc/ceph/ceph.conf

改cluster配置为Rdma
 sed -i 's/^ms_cluster_type[[:space:]]\+=.*/ms_cluster_type = async+rdma/' /etc/ceph/ceph.conf

改public_io为rdma
sed -i 's/^ms_public_io_type[[:space:]]\+=.*/ms_public_io_type = async+rdma/' /etc/ceph/ceph.conf

改public_io为posix
sed -i 's/^ms_public_io_type[[:space:]]\+=.*/ms_public_io_type = async+posix/' /etc/ceph/ceph.conf



 sed -i 's/^ms_public_type[[:space:]]\+=.*/ms_public_type = async+posix/' /etc/ceph/ceph.conf
 sed -i 's/^ms_cluster_type[[:space:]]\+=.*/ms_cluster_type = async+posix/' /etc/ceph/ceph.conf
 sed -i 's/^ms_public_io_type[[:space:]]\+=.*/ms_public_io_type = async+posix/' /etc/ceph/ceph.conf





集群:

显示配置

line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS}  "cat /etc/ceph/ceph.conf |grep -E 'ms_public_io_type|ms_public_type|ms_cluster_type'"



改配置项:
 改public配置为Posix

line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sed -i 's/^ms_public_type[[:space:]]\+=.*/ms_public_type = async+posix/' /etc/ceph/ceph.conf"

 改public配置为rdma
line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sed -i 's/^ms_public_type[[:space:]]\+=.*/ms_public_type = async+rdma/' /etc/ceph/ceph.conf"

改cluster配置为Posix
line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sed -i 's/^ms_cluster_type[[:space:]]\+=.*/ms_cluster_type = async+posix/' /etc/ceph/ceph.conf"

改cluster配置为Rdma
 
 line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sed -i 's/^ms_cluster_type[[:space:]]\+=.*/ms_cluster_type = async+rdma/' /etc/ceph/ceph.conf"



显示ceph
line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sudo ps -aux|grep -E 'ceph-/*|dse'|grep -v 'grep';echo '=============================================================================='"

restart ceph
line=`cat /etc/ceph/ceph.conf |grep -E 'mon_host'`&&HOSTS=$(echo ${line#*=}|sed s/[[:space:]]//g)&&sudo clush -l root -w ${HOSTS} "sudo systemctl restart ceph.target"

如何在sed中使用变量

第一

在sed条件中是不认识变量取值的

sed '/$x/d' test

所以要想它能够识别变量

sed "/$x/d/" test

方法简单就是把"单引号"变成"双引号"

第二

teststr="IBM"

sed -n '/' "$teststr" '/=' testfile.txt

通常,我们使用sed进行变量替换的时候,替换和被替换变量都是hard-coded的。例如:

sed -n '/comm/p' /tmp/test.log

如果我们用一变量var,它的值根据上下文变化

$ var="comm",定义了变量,那么我们在sed的使用中这样使用变量

$ sed -n ‘/’”$var”‘/p’ /tmp/test.log

注意,是用单引号包含双引号来引用变量。

另:反引号可直接引用变量

例  KEYWORD_SUM=`cat $RUNNING_PATH/keywordfile.txt|wc -l `

awk 获取指定IP对应的网口

[shell] awk 获取指定IP对应的网口;https://blog.csdn.net/llm_hao/article/details/108654916

IP_ADDRESS=192.168.130.12 INTERFACE_NAME=`ifconfig | awk -F ":" '/'$IP_ADDRESS'/{print a}{a=$1}'`

使用shell获取网口名称

for i in `ls /sys/class/net`
do
echo -n `ip a show "$i" | awk '$1 ~ /inet$/' | awk -F "[/ ]+" '{print $NF," -- ",$3," (IPV4)"}'`
ip a show "$i" | awk '$1 ~ /inet6$/' | awk -F "[/ ]+" '{print " ",$3," (IPV6)"}'
done

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐