您的位置:首页 > 运维架构 > Shell

shell技巧

2015-10-30 11:06 429 查看
#grep -v "#" /etc/zabbix/zabbix_agentd.conf|grep -v "^$" 代表^$空格,-v代表不显示

获取脚本当前目录:BUILD_PATH=$(cd "$(dirname "$0")"; pwd)


pwd 是当前用户所在路径
比如Dev-FandeiMac:~ code-pc$ /tmp/t.sh 1 2
$0:/tmp/t.sh
pwd:~
$(dirname "$0"):/tmp


获取文本内容的第几行


wc -l a.txt 统计a.txt 行数
查看文件a.txt的第190行到196行,

sed -n '190,196p' a.txt
如果查看某一行用

sed -n '190,1p' a.txt
sed -n 'a,bp' a.txt 读取自第a行到第b行的数据

if(a > b ) return 第a行


实现变量+1

i=`expr $i + 1`

ret=$(cat /etc/ansible/hosts |awk '{if($1=="'$1'"){print$1" "$2}}')


输出第一列符合某值的行的第一二列

其中"'$1'"代表外部变量,不是指第一列



a 追加内容 sed ‘/匹配词/a\要加入的内容’ example.file(将内容追加到匹配的目标行的下一行位置)

i 插入内容 sed ‘/匹配词/i\要加入的内容’ example.file 将内容插入到匹配的行目标的上一行位置)

示例:

#我要把文件的包含“chengyongxu.com”这个关键词的行前或行后加入一行,内容为“allow chengyongxu.cn”

行前加


sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' the.conf.file

行前后

sed -i '/allow chengyongxu.com/a\allow chengyongxu.cn' the.conf.file

sed -i '/1/a\t' t.txt

在t.txt中找到一行内容为1,然后再改行后面写入t(mac不能用)



在一行的两段添加内容



sed 's/$/&tail/g' test.txt 每行尾添加tail




sed 's/^/head&/g' test.txt 每行头添加head





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: