文本处理工具

来源:互联网 发布:php支付接口开发demo 编辑:程序博客网 时间:2024/05/24 06:21
1.diff命令;用于
    cd /mnt
    touch file file1
    echo hello > file
    echo hello i love you > file
    vimdiff file file1         //用于比较两个文件的不同
    diff -u file file1 > file.path    //生成补丁文件
    yum install patch -y        //
    patch file file.path        //用与将补丁文件添加
2.grep命令
    grep root /etc/passwd        //过滤/etc/passwd中含有root的项
    grep root /etc/passwd -v    
    grep -i root /etc/passwd    //不区分大小写
    grep -n root /etc/passwd    //显示行号
    grep -c root /etc/passwd    //行数
    grep -r root 目录        //对文件执行第归搜索,从命名目录开始
    grep root passwd | grep ^root -v | grep root$ -v    //只显示中间的
    含有root
3,cut命令    
    ifconfig eth0 |head -n 2 |tail -n 1 |cut -c 13-27    //只显示
    ip    
4.sort命令;    
    sort -r    file                //将文件内容首字母排序
         -n                    //倒序排列
         -u                    //去重复值
    sort -rn file |uniq -c            //显示每个数的个数
    sort -rn file |uniq -d            //显示重复的数字
    sort -rn file |uniq -u            //显示不重复的数字
    sort -t : -k 3 -n file    //-t指定分隔符:,-k 3表示第三列,-n显示
5.tr命令
    tr 'a-z' 'A-Z' < file            //将文件中的小写替换成大写
    tr 'A-Z' 'a-z' < file            //将文件中的大写替换成小写
6.sed命令
    sed ‘s/sbin/westos/g’ passwd        //将全文的sbin换成westos
    只是查看是改变,并不会存入文件
    sed ‘s/sbin/westos/g’ -i passwd        //将全文的sbin换成westos并存入文
    件中
    sed ‘1,5s/sbin/westos/g’ passwd    //1到5行的作替换
    sed ‘/lp/,/halt/s/sbin/westos/g’ passwd  //将全文从“lp”到“halt”中间的替
    换
    以文件指定的方式转换:
    vim linux
        s/sbin/westos/g
        s/nologin/linux/g
    sed -f linux file
    sed -e 's/sbin/westos/g' -e 's/nologin/linux/g' file    //命令方式替换
    多处
    sed -n 5p file            //只显示第五行
    sed 5d file            //删除第五行
    sed 5p file            //复制第五行并显示
0 0
原创粉丝点击