• sed 命令运用


    sed 命令:
    echo "I am oldboy,my qq is 1234567" >>oldboy.txt
    要求:
    1、取出oldboy.txt 文件中的oldboy字符串
    方法一:
    [root@git oldboy]# echo "I am oldboy,my qq is 1234567">oldboy.txt
    [root@git oldboy]# cat oldboy.txt
    I am oldboy,my qq is 1234567
    [root@git oldboy]# sed 's#^.*m ##g' oldboy.txt
    oldboy,my qq is 1234567
    [root@git oldboy]# sed 's#^.*m ##g' oldboy.txt |sed 's#,.*##g'
    oldboy
    [root@git oldboy]# sed 's/^.*m //g' oldboy.txt|sed 's/,.*//g'
    oldboy
    注意:"#"和"/"都是替换的符号
    方法二:
    [root@git oldboy]# sed 's#^.*m (oldboy),.*#1#g' oldboy.txt
    oldboy

    [root@git oldboy]# sed 's#^.*m (old)(boy),.*#1#g' oldboy.txt
    old
    [root@git oldboy]# sed 's#^.*m (old)(boy),.*#12#g' oldboy.txt
    oldboy
    [root@git oldboy]# sed 's#^.*m (old)(boy),.*#2#g' oldboy.txt
    boy
    后面加数字的意思是取出前面括号里面的内容,第一个括号表示1,第二个表示2,以此类推。

    要求:
    [root@git oldboy]# cat oldboy.txt
    I am oldboy,my qq is 1234567
    I am old,but am strong
    welcome to test
    去除oldboy.txt中含old不含oldboy的行
    方法一:
    [root@git oldboy]# sed '/old[^boy]/d' oldboy.txt
    I am oldboy,my qq is 1234567
    welcome to test
    方法二:
    [root@git oldboy]# sed '/<old>/d' oldboy.txt
    I am oldboy,my qq is 1234567
    welcome to test
    "<>"匹配单个字符
    将old不含boy的行替换为www.baidu.com
    [root@git oldboy]# sed 's#old[^boy]#www.baidu.com#g' oldboy.txt
    I am oldboy,my qq is 1234567
    I am www.baidu.combut am strong
    welcome to test
    [root@git oldboy]# sed 's#<old>#www.baidu.com#g' oldboy.txt
    I am oldboy,my qq is 1234567
    I am www.baidu.com,but am strong
    welcome to test

  • 相关阅读:
    [转]为iPhone4S和iOS5增加全局英汉词典教程
    Notes on MSBuild
    Using jQuery To Manipulate and Filter Data
    使用MSBuild进行自动化构建
    使用ADO.NET的计算列
    来自Twitter的前端工具包——Bootstrap
    Jquery Templetes简介
    C#导入导出Excel
    Mysql在sql中截取时间类型字段的年月日和时间
    使用NPOI导出Excel,并在Excel指定单元格插入图片
  • 原文地址:https://www.cnblogs.com/cheerong/p/4133493.html
Copyright © 2020-2023  润新知