zoukankan      html  css  js  c++  java
  • 笔记02 linux的一些命令sed

    #!/bin/bash
    #
    dataformat=`date +%Y-%m-%d-%H-%M`
    #进行文件件cp并重命名
    nginx_home=/opt/modules/nginx-1.12/
    cp  ${nginx_home}logs/access.log  ${nginx_home}logs/access_$dataformat.log
    host=`hostname`
    #在每行之前插入nginx集群的主机名,便于区分那个机器的日志
    sed -i 's/^/'${host}',&/g' ${nginx_home}logs/access_$dataformat.log
    #计算出复制出来的文件有多少行
    lines=`wc -l < ${nginx_home}logs/access_$dataformat.log`
    #move access-xxx.log flume's spooldir
    mv ${nginx_home}logs/access_$dataformat.log ${nginx_home}logs/flume
    #delete rows 删除1到原先的行数就是剩的的新的行数
    sed -i '1,'${lines}'d' ${nginx_home}logs/access.log
    #reboot nginx , otherwise log can not roll.
    kill -USR1 `cat ${nginx_home}logs/nginx.pid`
    
    */1 * * * *  /bin/sh  /opt/cron_shell/roollog.sh  > /opt/cron_shell/shell_log
    
    
    使用命令:
    sed    -n    np    path
    sed:命令
    -n:选项
    np:参数,取第几行内容
    path:文件路径
    
    如,取/home/xx/Makefile的第7行内容:
    sed -n 7p /home/xx/Makefile
    
    取多行内容:8-20行的
    sed -n 8,20p /home/xx/Makefile
    
    可以配合其他命令使用,获取/home/xx/Makefile中包含内容"arm"的第一行,注意大小写:
    cat /home/xx/Makefile | grep "arm" | sed -n 1p
    
    替换方法2:
    Linux下批量替换多个文件中的字符串的简单方法。用sed命令可以批量替换多个文件中的字符串。
    用sed命令可以批量替换多个文件中的 字符串。 
    sed -i "s/原字符串/新字符串/g" `grep 原字符串 -rl 所在目录`
    例如:我要把mahuinan替换 为huinanma,执行命令: 
    sed -i "s/mahuinan/huinanma/g" 'grep mahuinan -rl /www'
    这是目前linux最简单的批量替换字符串命令了!
    具体格式如下: 
    sed -i "s/oldString/newString/g"  `grep oldString -rl /path`
    实例代码:sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl /usr/aa`
    sed -i "s/大小多少/日月水火/g" `grep 大小多少 -rl ./`
  • 相关阅读:
    SpringBoot学习笔记
    2021牛客多校第一场 I题(DP)
    CSS小结
    AOP小结
    IOC容器小结
    Educational Codeforces Round 56 (Rated for Div. 2) G题(线段树,曼哈顿距离)
    Codeforces Round #656 (Div. 3) E. Directing Edges(拓扑排序)
    Educational Codeforces Round 101 (Rated for Div. 2) E
    [FJOI2017]矩阵填数 (容斥原理)
    优秀代码样板收集计划(python)
  • 原文地址:https://www.cnblogs.com/hejunhong/p/10527015.html
Copyright © 2011-2022 走看看