sed好用的命令

1.创建多个目录

# mkdir 7001 7002 7003 8001 8002 8003
[test@host1 tmp]$ ll
total 0
drwxrwxr-x 2 test test 6 Oct 29 10:58 7001
drwxrwxr-x 2 test test 6 Oct 29 10:58 7002
drwxrwxr-x 2 test test 6 Oct 29 10:58 7003
drwxrwxr-x 2 test test 6 Oct 29 10:58 8001
drwxrwxr-x 2 test test 6 Oct 29 10:58 8002
drwxrwxr-x 2 test test 6 Oct 29 10:58 8003

准备一个配置文件tmp.conf。将配置文件复制进不同的目录下

# cat tmp.conf 
6379

2.管道方式复制文件

# echo 7001 7002 7003 8001 8002 8003 | xargs -t -n 1 cp tmp.conf
cp tmp.conf 7001 
cp tmp.conf 7002 
cp tmp.conf 7003 
cp tmp.conf 8001 
cp tmp.conf 8002 
cp tmp.conf 8003 

3.结果

[test@host1 tmp]$ ll 7001/
total 0
-rw-rw-r-- 1 test test 0 Oct 29 11:03 tmp.conf
[test@host1 tmp]$ ll 7002
total 0
-rw-rw-r-- 1 test test 0 Oct 29 11:03 tmp.conf

4.修改文档中某行数据

# printf '%s\n' 7001 7002 7003 8001 8002 8003 | xargs -I {} -t sed -i 's/6379/{}/g' {}/tmp.conf
sed -i s/6379/7001/g 7001/tmp.conf 
sed -i s/6379/7002/g 7002/tmp.conf 
sed -i s/6379/7003/g 7003/tmp.conf 
sed -i s/6379/8001/g 8001/tmp.conf 
sed -i s/6379/8002/g 8002/tmp.conf 
sed -i s/6379/8003/g 8003/tmp.conf 
# cat 7001/tmp.conf 
7001
# cat 8003/tmp.conf 
8003

5.实际使用时,也可以这样

# 1. 创建多个配置文件副本
for port in 7001 7002 7003; do
    cp redis.conf ${port}/redis.conf
done

# 2. 批量替换端口号
printf '%s\n' 7001 7002 7003 | xargs -I {} -t sed -i 's/6379/{}/g' {}/redis.conf

命令将文件都放在不同的目录下了。但我们想用端口号将配置文件区分下

6.批量启动

printf '%s\n' 7001 7002 7003 8001 8002 8003 | xargs -I{} -t redis-server {}/redis.conf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值