实用的bash脚本


实用的bash脚本
 
ping局域网中所有的机器
 
for i in `seq 1 254`;do (ping 192.168.0.$i &);sleep 0.1;killall ping;done
 
for i  in `seq 1 10`;do ping 10.228.151.$i -c1;done|grep ttl
批量在客户端添加hosts对应关系
 
#!/bin/bash
 
for ((i=2; i<=100; i++))
 
do
 
        _ip=10.11.45.$i
 
        ssh $_ip "echo \"${_ip}       \"\$(hostname) >> /etc/hosts"
 
done
 
客户端命令执行工具
 
vim batch_run.sh
 
#!/bin/bash
 
# wangyang
 
usage ()
 
{
 
        echo "Usage $0 list_file \"command\""
 
        exit 1
 
}
 
 
 
if [[ $# != 2 ]]; then
 
        usage
 
fi
 
 
 
_file=$1
 
_command=$2
 
 
 
if [ ! -f $_file ]; then
 
        echo "ERR can't find list file $_file"
 
        exit 2
 
fi
 
 
 
for _ip in $(cat $_file|grep -v "^#")
 
do
 
        echo $_ip
 
        ssh $_ip "$_command"
 
        echo
 
done
 
vim hosts.txt
 
192.168.0.2
 
192.168.0.3
 
192.168.0.4
 
例子:
 
#batch_run.sh  hosts.txt  "cat /etc/hosts"
 
 
 
文件分发脚本
 
vim batch_cp.sh
 
#!/bin/bash
 
usage ()
 
{
 
        echo "Usage $0 list_file local_file remote_path"
 
        exit 1
 
}
 
if [[ $# != 3 ]]; then
 
        usage
 
fi
 
_file=$1
 
_lf=$2
 
_rp=$3
 
if [ ! -f $_file ]; then
 
        echo "ERR can't find list file $_file"
 
        exit 2
 
fi
 
for _ip in $(cat $_file|grep -v "^#")
 
do
 
        echo $_ip
 
        scp -r $_lf $_ip:$_rp
 
        echo
 
done
 
vim hosts.txt
 
192.168.0.2
 
192.168.0.3
 
192.168.0.4
 
例子:
 
#./batch_cp.sh hosts.txt  /tmp/aaa   /root/  
 
 
 
删除文本中得重复行
 
在进行文本处理的时候,我们经常遇到要删除重复行的情况。那怎么解决呢?
下面就是三种常见方法?
第一,用sort+uniq,注意,单纯uniq是不行的。
 
shell> sort file | uniq
 
这里我做了个简单的测试,当file中的重复行不再一起的时候,uniq将服务删除所有的重复行。经过排序后,所有相同的行都在相邻,因此unqi可以正常删除重复行。第二,用sort+awk命令,注意,单纯awk同样不行,原因同上。
 
 shell> sort file | awk '{if ($0!=line) print;line=$0}'
 
awk '!a[$0]++' fetion_info.txt
 
当然,自己把管道后面的代码重新设计一下,可能不需要sort命令先排序拉。第三,用sort+sed命令,同样需要sort命令先排序。
 
shell> sort file | sed '$!N; /^(.*)n1$/!P; D'
最后附一个必须先用sort排序的文本的例子,当然,这个需要用sort排序的原因是很简单,就是后面算法设计的时候的“局部性,相同的行可能分散出现在不同的区域,一旦有新的相同行出现,那么前面的已经出现的记录就被覆盖了
 
 
 
 
 
comm --取两个文件的差集合并集
 
今天需要取两个文件的并集,自己写脚本处理太麻烦,一查,果然有现成的工具,comm。
需要注意的事,使用comm之前,两个文件都是必须是sort好了的。
 
In our work, we often encounter the following questions:
在我们的工作中,我经常遇到下面的问题:
I have two files: file1 and file2:
有两个文件:文件1和文件2:
1) How can I print out the lines that are only contained in file1?
1) 如何打印出只存在于文件1中的内容?
2) How can I print out the lines that are only contained in file2?
2) 如何打印出只存在于文件2中的内容?
3) How can I print out the lines that are contained both in file1 and file2?
3) 如何打印出文件1和文件2都有的内容?
 
There is a powerful shell command that can easily meet our needs, it is: comm. 
这有一个很好的shell命令能够满足我们的需求,它就是comm。
 
When you meet the above questions, "comm" should be your first choice:-)
当你遇到上面的问题,“comm”应该是你第一选择:-)
 
comm [ -123 ]??file1??file2
 
comm will read file1 and file2 and generate three columns of output: 
comm 将会读取文件1和文件2并且产生三列输出:
lines only in file1; lines only??in file2; and lines in both files. 
只存在文件1中的行;只存在文件2中的行;两个文件都存在的行。
For detailed explanation, pls man comm.
更详细的解释,请参阅man comm。
 
Example:
例如:
 
bash-2.03$ cat file1
11111111
22222222
33333333
44444444
55555555
66666666
77777777
88888888
99999999
bash-2.03$ cat file2
00000000
22222222
44444444
66666666
88888888
 
1) suppress lines unique to FILE1
1) 过滤掉file1中的内容
bash-2.03$ comm -1 file1 file2 
00000000
        22222222
        44444444
        66666666
        88888888
 
2) suppress lines unique to FILE2
2) 过滤掉file2中的内容
bash-2.03$ comm -2 file1 file2 
11111111
        22222222
33333333
        44444444
55555555
        66666666
77777777
        88888888
99999999
 
3) suppress lines that appear in both files
3) 过滤掉file1和file2中都有的内容
bash-2.03$ comm -3 file1 file2 
        00000000
11111111
33333333
55555555
77777777
99999999
 
4) Print out the lines that are only contained in file1?
4) 打印出只存在于文件1中的内容?
bash-2.03$ comm -23 file1 file2
11111111
33333333
55555555
77777777
99999999
 
5) Print out the lines that are only contained in file2?
5) 打印出只存在于文件2中的内容?
bash-2.03$ comm -13 file1 file2
00000000
 
6) Print out the lines that are contained both in file1 and file2?
6) 打印出文件1和文件2都有的内容?
bash-2.03$ comm -12 file1 file2
22222222
44444444
66666666
88888888
 
Besides the comm, we still have various ways to finish the above tasks.
除了comm,我们还有其他方法来完成这些任务。
 
4) Print out the lines that are only contained in file1?
4) 打印出只存在于文件1中的内容?
diff file1 file2 | grep "^<"|sed 's/^< //g'
for i in $(>temp ; done;
cat temp

相关内容

    暂无相关文章