shell 自动化输入 uniq去除重复项

shell 学习(20230411)

1 uniq

uniq命令通过消除重复内容,从给定的输入中找到单一行。
例如:uniq.txt文档如下:

[root@spark10 linux_test]# cat uniq.txt
start
end
first
ok
ok
is
is
is

A、去除重复项: uniq uniq.txt

[root@spark10 linux_test]# uniq uniq.txt
start
end
first
ok
is

或者: sort -u uniq.txt(sort命令用于排序)

[root@spark10 linux_test]# sort -u uniq.txt
end
first
is
ok
start

B、每行重复的次数:uniq -c uniq.txt

[root@spark10 linux_test]# uniq -c uniq.txt
      1 start
      1 end
      1 first
      2 ok
      3 is

C、查看行有重复的:uniq -d uniq.txt

[root@spark10 linux_test]# uniq -d uniq.txt
ok
is

2 sed

A、sed ‘s/[^/n]/&\n/g’
在给个字符后面追加一个换行符,使得每行只出现一个字符

B、sed ‘/^s/d’
删除最后的空行

3 tr

tr -d ‘\n’
将输入的空格和换行符删除。

4 /tmp存放临时数据(/tmp目录的内存在系统重启后被清空)

5 生产随机120k大小的文件

dd if=/dev/zero bs=120k count=1 of=123.file

[root@spark10 ~]# dd if=/dev/zero bs=120k count=1 of=123.file
1+0 records in
1+0 records out
122880 bytes (123 kB) copied, 0.000314866 s, 390 MB/s

文件切割split
将文件123.file切割成10k大小,新的文件用xaa、xab、xac等命令
split -b 10k 123.file

[root@spark10 ~]# split -b 10k 123.file
[root@spark10 ~]# ls
123.file         dead.letter  Documents  Music            Pictures  Templates  xaa  xac  xae  xag  xai  xak
anaconda-ks.cfg  Desktop      Downloads  original-ks.cfg  Public    Videos     xab  xad  xaf  xah  xaj  xal
[root@spark10 ~]#

6 自动化输入

A、读取交互式脚本并演示(读取输入的用户名和密码)

[root@spark10 linux_test]# cat auto.sh
#!/bin/bash
#文件名:auto.sh
read -p "Enter username:" username ;
read -p "Enter Password:" passwd ;
echo You entered username is $username, Password is $passwd;
[root@spark10 linux_test]# echo -e "admin\npassword\n" | ./auto.sh
You entered username is admin, Password is password

B、将echo的输入重定向到文本

[root@spark10 linux_test]# echo -e "admin\npassword\n" > input.txt
[root@spark10 linux_test]# cat input.txt
admin
password

C、从文件中导入交互数据

[root@spark10 linux_test]# ./auto.sh < input.txt
You entered username is admin, Password is password
[root@spark10 linux_test]#

D、使用expect 脚本实现交互式自动化

[root@spark10 linux_test]# cat auto_expect.sh
#!/usr/bin/expect
#文件名:auto_expect.sh
spawn ./auto.sh
expect "Enter username:"
send "admin\n"
expect "Enter Password:"
send "password\n"
expect eof
[root@spark10 linux_test]# ./auto_expect.sh
spawn ./auto.sh
Enter username:admin
Enter Password:password
You entered username is admin, Password is password
[root@spark10 linux_test]#

spawn参数指定需要自动化执行的命令或者脚本
expect参数指定需要等待的消息,这里指:Enter username:
send参数是要发送的消息;这里指:admin\n
expect eof 参数指定交互结束

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值