cat 123.txt
1 4 7 9 4
2 5 8 3 8
3 6 9 7 6
#!/bin/bashsort_list(){num=($1)l=${#num[*]}for((i=0;i<$l;i++))dofor((j=0;j<$l-$i;j++))doone=${num[$j]}k=j+1two=${num[$k]}if [[ $one -gt $two ]]thennum[$k]=${one}num[$j]=${two}fidonedoneecho ${num[*]}}uniq_list(){un=($1)l=${#un[*]}for((i=0;i<$l;i++))do[ $i -eq 0 ] && echo -n "${un[$i]} "[ ${un[$i]} -gt ${un[$i-1]} ] && echo -n "${un[$i]} "doneecho}n=$(cat /data/123.txt|wc -l)for((x=1;x<=$n;x++)) doc=(`sed -n "${x}p" /data/123.txt`)C=`sort_list "${c[*]}"`uniq_list "${C[*]}"done#c=(`sed -n '1p' /data/123.txt`)#C=`sort_list "${c[*]}"`#uniq_list "${C[*]}"###b=(`sed -n '2p' /data/123.txt`)#B=`sort_list "${b[*]}"`#uniq_list "${B[*]}"###e=(`sed -n '3p' /data/123.txt`)#E=`sort_list "${e[*]}"`#uniq_list "${E[*]}" 方法二:[root@localhost /data]#cat sort.sh #!/bin/bashuniq_list(){num=($1)l=${#num[*]}for((i=0;i<$l;i++))do[ $i -eq 0 ]&& echo -n "${num[$i]} "[ ${num[$i]} -gt ${num[$i-1]} ]&& echo -n "${num[$i]} "doneecho}sort_list(){num=($1)l=${#num[*]}for((i=0;i<$l-1;i++))dofor((j=$i+1;$j<$l;j++))doif [ ${num[$i]} -gt ${num[$j]} ]thentmp=${num[$j]}num[$j]=${num[$i]}num[$i]=${tmp}fidonedoneecho ${num[*]}}n=$(cat /data/123.txt|wc -l)for((x=1;x<=$n;x++)) doc=(`sed -n "${x}p" /data/123.txt`)C=`sort_list "${c[*]}"`uniq_list "${C[*]}"done 6、假设 file.txt 内容如下,请在grep,egrep,sed,awk中至少2种命令输出有效的号码:987 456-1230和(123) 456-7890,要求至少要有一种方法使用正则表达式匹配完整的号码。cat file.txt
987-123-5430
987 456-1230
(123) 456-7890
方法一:[root@localhost /data]#egrep "(-|\) )+[1-6]{3}-[0-9]{4}" tel.txt 987-123-5430(123) 456-7890##长一点egrep "[()]?[0-9]{3}(-|\) )+[1-6]{3}-[0-9]{4}" tel.txt 方法二:[root@localhost /data]#sed -nr "/(-|\) )+[1-6]{3}-[0-9]{4}/p" tel.txt987-123-5430(123) 456-7890##长一点sed -nr "/[(]?[0-9]{3}(-|\) )+[1-6]{3}-[0-9]{4}/p" tel.txt 7.遍历/data/resources/下的文件,找出大于200M的文件,判断其若以“tar”结尾,则使用rm删除;若以“log“结尾,则清空该文件。 [root@localhost /data]#cat tarlog.sh #!/bin/bashfind_f(){file=(`ls $1`)for i in ${file[*]}do[[ $i == *.tar ]] && rm -rf ${1}/${i}[[ $i == *.log ]] && echo " " > ${1}/${i}[ -d $1/$i ] && find_f "${1}/${i}" ##这里是递归调用自己,因为是目录的话,需要在递归往下寻找done}find_f $1find_f "/data/resources"8.(4分)有一个文件b.txt,内容如下:
[root@localhost test]# cat b.txt http://www.baidu.com/index.html http://www.google.com/index.html http://www.baidu.com/get.html http://www.baidu.com/set.html http://www.google.com/index.html http://www.yahoo.com.cn/put.html
8、现要求使用 两种 方法将该文件中的域名截取出来,统计重复域名出现的次数,然后按次数进行降序排列,统计后的结果如下:3 www.baidu.com 2 www.google.com 1 www.yahoo.com.cn
方法一:cat b.txt |tr -s "/"|cut -d"/" -f2|sort|uniq -c方法二:cat b.txt |awk -F"/" '{print $3}'|sort|uniq -c方法三:表示把每一行进行分类,任意://(域名)/任意,只想要括号的内容\1cat b.txt |sed -nr "s@.*://(.*)/.*@\1@p"|sort|uniq -c3 www.baidu.com2 www.google.com1 www.yahoo.com.cn