sed命令使用

        sed -n -e '/python/p' -e 'PYTHON/p' sed.txt
        sed -n -f edit.sed sed.txt
        sed -n -r '/python|PYTHON/p' sed.txt
        sed -i 's/love/like/g' sed.txt 

        sed -n "17p" sed.txt
        sed -n "10,20p"    sed.txt
        sed -n "10,+5p"    sed.txt
        sed -n "/^root/p" sed.txt
        sed -n "/^ftp/,/^mail/p" sed.txt    --打印以ftp开头的行,到以mail开头的行
        sed -n "4,/^mail/p" sed.txt    --打印从第4行开始,直到匹配到以mail开头的行结束
        sed -n "/root/,10p"        --打印匹配到root的行开始,到第10行结束 

   查询:
        1、p    ------打印
    
    删除
        2、d    ------删除
        sed '1d' passwd
        sed -i '1d' passwd
        sed -i '1,3d' passwd
        sed -i '/\/sbin\/nologin/d' passwd
        sed -i '/^mail/,/^ftp/d' passwd
    增加
        3、a    ------匹配到行后追加内容
        sed -i '/\/bin\/bash/a This is user which can login to system' passwd
        4、i    ------匹配到行前追加内容
        sed -i '/^mail/,/^ftp/i Ni Hao' passwd
        5、r    ------将后面指定文件内容追加到匹配到的行后面
        sed -i '/root/r list' passwd
        6、w    ------将匹配到的行内容另存到其他文件中
        sed -n '/\/bin\/bash/w /tmp/user.txt' passwd
    修改
        7、s/pattern/string/    ------查找并替换,只替换第一个
           sed -i 's/root/ROOT/' passwd
           s/pattern/string/g    ------全部替换
           sed -i 's/\/bin\/bash/\/BIN\/BASH/g' passwd 
           s/pattern/string/2g    ------替换从第二个开始,后面的全部替换
           sed -i 's/root/ROOT/2g' passwd
           s/pattern/string/ig    ------不区分大小写,全部替换
           sed -i 's/root/ROOT/ig' passwd
    
反向引用:
    &和\1        引用模式匹配到的整个串
    sed 's/1..e/&j/g' file        --搜索file中以l开头,然后跟两个任意字符,以e结尾的字符串
    sed 's/\(1..e\)/\1j/g' file    --和上面实现一样的功能,使用\1代表搜寻到的字符串
    两者的区别在于&只能表示匹配到的完整的字符串,只能引用整个字符串;而\1可以使用()对匹配到的字符串进行处理。
    
sed中引用变量时需要注意事项
1、匹配模式中存在变量时,则建议使用双引号。
    sed -i "s/$old_str/$new_str/g" str.txt
2、sed中需要引用自定义变量时,如果外面使用单引号,则自定义变量也必须使用单引号。
    sed -i 's/'$old_str'/'$new_str'/g' str.txt

sed查找文件特定内容:
需求描述:处理一个类似Mysql配置文件my.cnf的文本,示例如下;编写脚本实现以下功能:输出文件有几个段,并且针对每个段可统计配置参数总个数
#this is read by the standalone daemon and embedded servers
[client]
port=3306
socket=/tmp/mysql.socket
#ThisSegmentForserver
[server]
innodb_buffer_pool_size=91750M
innodb_buffer_pool_instances=8
innodb_buffer_pool_load_at_startup=1
innodb_buffer_pool_dump_at_shutdown=1
innodb_data_file_path=ibdata1:1G:autoextend
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=32M
innodb_log_file_size=2G
innodb_log_files_in_group=2
innodb_max_undo_log_size=4G
innodb_undo_directory=undolog
innodb_undo_tablespaces=95
#thisisonlyforthemysqldstandalonedaemon
[mysqld]
port=3306
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/data/mysql
pid-file=/data/mysql/mysql.pid
user=mysql
bind-address=0.0.0.0
sort_buffer_size=16M
join_buffer_size=16M
thread_cache_size=3000
interactive_timeout=600
wait_timeout=600
#ThisSegmentFormysqld_safe
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
max_connections=1000
open_files_limit=65535
thread_stack=512K
external-locking=FALSE
max_allowed_packet=32M
#thisisonlyforembeddedserver
[embedded]
gtid_mode=on
enforce_gtid_consistency=1
log_slave_updates
slave-rows-search-algorithms=‘INDEX_SCAN,HASH_SCAN’
binlog_format=row
binlog_checksum=1
relay_log_recovery=1
relay-log-purge=1
#usethisgroupforoptionsthatolderserversdon’tunderstand
[mysqld-5.5]
key_buffer_size=32M
read_buffer_size=8M
read_rnd_buffer_size=16M
bulk_insert_buffer_size=64M
myisam_sort_buffer_size=128M
myisam_max_sort_file_size=10G
myisam_repair_threads=1
lock_wait_timeout=3600
explicit_defaults_for_timestamp=1
innodb_file_per_table=1


脚本练习 - 需求分析
1、获取文件中所有的段 function get_all_segment利用sed 命令 通过 ‘[…]‘过滤出所有的段,同时替换掉’[‘与’]’,并输出
2、获取文件中每一段的配置项的个数 function count_items_in_segment利用 sed 命令提取两个段之间的内容,同时还需过滤掉两段之间的空白行与注释行,得到的就是配置项的信息。

#!/bin/bash
FILE_NAME=my.cnf
function get_all_segments
{
echo "`sed -n '/\[.*\]/p' $FILE_NAME | sed -e 's/\[//g' -e 's/\]//g'`"
}
function count_items_in_segment
{
items=`sed -n '/\['$1'\]/,/\[.*\]/p' $FILE_NAME | grep -v "^#" | grep -v "^$" | grep -v "\[.*\]"`
index=0
for item in $items
do
index=`expr $index + 1`
done
echo $index
}
number=0
for segment in `get_all_segments`
do
number=`expr $number + 1`
items_count=`count_items_in_segment $segment`
echo "$number: $segment $items_count"
done

sed删除特定内容:
典型需求:
1、删除配置文件中所有注释行和空行
    sed -i '/[:blank]*#/d;/^$/d' nginx.conf
2、在配置文件中所有不以#开头的行前面添加*,注意:以#开头的行不添加
    sed -i 's/^[^#]/\*&/g' nginx.conf

sed修改文件内容:
sed -i '1s/root/ROOT/' passwd
sed -i '5,10s/\/sbin\/nologin/\/bin\/bash/g' passwd
sed -i '/\/sbin\/nologin/s/login/LOGIN/g' passwd
sed -i '/^root/,/mail/s/bin/HADOOP/g' passwd
sed -i '/^root/,15s/nologin/SPARK/g' passwd
sed -i '15,/^yarn/s/bin/BIN/g' passwd

sed -i 's/[0-9]*//g' file.txt

sed追加文件内容:
sed -i '10a Add' passwd
sed -i '10,20a Test' passwd
sed -i '/\/bin\/bash/a Insert' passwd 

sed -i '/^yarn/i Add' passwd
sed -i 'i Insert Line' passwd

sed -i '20r /etc/fstab' passwd    --将/etc/fstab文件内容追加到passwd文件的第20行后面
sed -i '/\/sbin\/nologin/r /etc/inittab' passwd
sed -i '/^ftp/,18r /etc/vconsole.conf' passwd

sed -i '/\/bin\/bash/w /tmp/sed.txt' passwd
sed -i '10,/^hdfs/w /tmp/sed-1.txt' passwd

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值