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
获取脚本当前目录: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
相关文章推荐
- shell 脚本完成对日志文件的提取
- shell命令getopts
- shell命令getopts
- 如何使用PowerShell操作MySQL
- Linux-shell学习笔记
- 使用git bash 代替cmd
- Bash Shell编程要点小结
- 什么是shell,shell基础由浅入深,常用的shell命令、用法、技巧
- ubuntu操作系统写shell的for循环会遇到的问题
- sqlite3 shell的使用
- sqlite3 shell的使用
- shell脚本实例参考
- shell script 学习笔记-----标准输出
- Shell脚本之监视主进程的运行状态,异常退出后自动重启
- shell脚本:打印文本中字符数不大于6的单词
- shell脚本:打印文本中字符数不大于6的单词
- 《Linux Shell》笔记之sed与正则表达式(实例总结) 未完待续
- 一定要记住的一些shell命令
- linux shell 基础 使用日志与心得
- 安装ssh命令