linux shell 修改文本
echo

[root@DSI tmp]# echo 'yhqt1 test1' > test1.txt
[root@DSI tmp]# cat test1.txt
yhqt1 test1
[root@DSI tmp]# echo 'yhqt2 test2' > test1.txt
[root@DSI tmp]# cat test1.txt
yhqt2 test2
[root@DSI tmp]# echo 'yhqt1 test1' >> test1.txt ##追加
[root@DSI tmp]# cat test1.txt
yhqt2 test2
yhqt1 test1

##增加文本

[root@DSI tmp]# cat >> test1.txt << EOF
export HISTTIMEFORMAT='%F %T '
EOF

sed

[root@DSI tmp]# sed -i '$a test3' test1.txt ##$最后一行,a是新增
[root@DSI tmp]# cat test1.txt
yhqt2 test2
yhqt1 test1
test3
[root@DSI tmp]# sed '/yhqt1/a\test4' test1.txt ##在yhqt1 行后面增加一行test4
yhqt2 test2
yhqt1 test1
test4
test3
[root@DSI tmp]# sed '/yhqt1/a\test5\ntest6' test1.txt ##在yhqt1 行后面增加2行
yhqt2 test2
yhqt1 test1
test5
test6
test3
[root@DSI tmp]# sed '/yhqt1/i\test7' test1.txt ##在yhqt1行前面增加一行test7
yhqt2 test2
test7
yhqt1 test1
test3
[root@DSI tmp]# cat test1.txt
yhqt2 test2
test7
yhqt1 test1
test5
test6
test4
test3
test7
[root@DSI tmp]# sed -i '/test7/a\1111' test1.txt ##存在多行test7的情况,每个匹配的地方都会新增一行1111数据
[root@DSI tmp]# cat test1.txt
yhqt2 test2
test7
1111
yhqt1 test1
test5
test6
test4
test3
test7
1111
##如果只向第二个test7后面增加一行,可以先获取第二个test7的行号,然后根据此行号在后面增加一行数据
##获取行号
[root@DSI tmp]# cat -n test1.txt |grep test7 |awk ' {print $1}'|sed -n "2"p
9
[root@DSI tmp]# sed -n '/test7/=' test1.txt |sed -n "2"p
9
[root@DSI tmp]# sed -e '9a\2222' test1.txt
yhqt2 test2
test7
1111
yhqt1 test1
test5
test6
test4
test3
test7
2222
1111
[root@DSI tmp]# sed 's/test5/& yhq1314/g' test1.txt ##在指定行test5后面增加数据yhq1314
yhqt2 test2
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111
[root@DSI tmp]# sed -i 's|test2|test222|' test1.txt ##对字符串进行替换test2替换为test222
[root@DSI tmp]# cat test1.txt
yhqt2 test222
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111
[root@DSI tmp]# sed -i '$a yhq,abc1,3456' test1.txt
[root@DSI tmp]# cat test1.txt
yhqt2 test222
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111
yhq,abc1,3456
[root@DSI tmp]# sed -i 's|,|*|' test1.txt ##替换特殊字符
[root@DSI tmp]# cat test1.txt
yhqt2 test222
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111
yhq*abc1,3456

sed是stream editor(流编辑器)的缩写,是文本处理中非常重要的工具,配合正则表达式进行使用功能更强大。
sed可以替换给定文本中的字符串,可以利用正则表达式进行匹配

$ sed 's/pattern/replace_string/' file

或者

$ cat file |sed 's/patter/replaces_string/' file

使用 -i选项,可以将替换结果应用于原文件,很多在进行替换之后,借助重定向来保存文件

$ sed 's/text/replace/' file > newfile
$ mv newfile file

其实就是一个命令
$ sed -i 's/text/replace/' file
如果需要替换每一行中满足条件的内容,加参数g
$ sed 's/pattern/replace_string/g' file
后缀/g意味着sed会替换每一处匹配,如果需要从N+1开始匹配,加参数N

[root@DSI tmp]# echo this thisthisthis | sed 's/this/THIS/2g'
this THISTHISTHIS
[root@DSI tmp]# echo this thisthisthis | sed 's/this/THIS/3g'
this thisTHISTHIS
[root@DSI tmp]# echo this thisthisthis | sed 's/this/THIS/4g'
this thisthisTHIS

#当需要从第N出匹配开始替换时,可以使用/Ng
字符/在sed中作为定界符使用。可以像下面一样
sed 's:text:replace:g'
sed 's|text|replace|g'
当定界符出现在样式内部时,必须使用前缀\对它进行转义
sed 's|te\|xt|replace|g'
\|是一个出现在样式内部并经过转义的定界符

1 移除空白行

$ sed '/^$/d' file ##/pattern/d会移除匹配样式的行,在空白行中,行尾标记紧随着行首标记
[root@DSI tmp]# cat test1.txt
yhqt2 test222
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111 yhq*abc1,3456
xxx [root@DSI tmp]# sed -i '/^$/d' test1.txt
[root@DSI tmp]# cat test1.txt
yhqt2 test222
test7
1111
yhqt1 test1
test5 yhq1314
test6
test4
test3
test7
1111
yhq*abc1,3456
xxx

2 已匹配字符串标记&
在sed中,用&标记匹配样式的字符串,就能够在替换字符串时使用已匹配的内容

[root@DSI tmp]# echo this is an example | sed 's/\w\+/[&]/g'
[this] [is] [an] [example]
##正则表达式\w\+ 匹配每一个单词,然后用[&]替换它,&对应于之前所匹配到的单词

3 子串匹配标记\1
&代表匹配给定样式的字符串,但也可以匹配给定样式的其中一部分

[root@DSI tmp]# echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
this is 7 in a number
##这个命令将digit 7替换为7.样式中匹配到的子串是7,\(pattern\)用于匹配子串,模式被包括在使用斜线转义过的()中,对于匹配到的第一个子串,
其对应的标记是\1,匹配到的第二个子串是\2,往后依次类推
[root@DSI tmp]# echo seven EIGHT | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
EIGHT seven
##([a-z])\+\)匹配第一个单词,\([A-Z]\+\)匹配第二个单词,\1,\2用来引用他们,这种引用称为向后引用,在替换部分,他们的次序被更改
为\2\1,因此结果就是逆序

4 组合多个表达式
sed 'expression' | sed 'expression'
等价于
$ sed 'expression; expression'
5 引用
sed表达式通常用单引号来引用。也可以使用双引号。双引号会通过对表达式求值来对其进行扩展

[root@DSI tmp]# text=hello
[root@DSI tmp]# echo hello world | sed "s/$text/HELLO/" ##$text的求值结果是hello
HELLO world

linux shell 修改文本 sed的更多相关文章

  1. Linux Shell处理文本最常用的工具大盘点

    导读 本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实 ...

  2. linux shell grep/awk/sed 匹配tab

    处理文件的命令实在是多, sed, awk, grep等.遇到了需要匹配tab的情况, 记录一下. 例子如下:找出文本中第一列是1的行. 文本a 解法1 : 直接使用正则表达式, ^表示开头, \t表 ...

  3. linux shell 去掉文本处理中的双引号

    cat aa.txt |sed 's/\"//g'  结果是:hello aa.txt "hello"

  4. linux shell中使用sed命令

    例1:批量的将变量的值代替指定文件中的指定内容. #!/bin/bash for i in {1..100} mgr_port=`expr $i + 5345` data_port=`expr $i ...

  5. Linux shell - 修改文件所属用户和组 (chown, chgrp)

    在工作中,会遇到这样的情况,需要把目录所属的的root用户更改到普通用户,root组更改到普通组. sha-q:/ # ll drwxr-xr-x 2 root root 4096 2014-09-1 ...

  6. Linux Shell 文本处理工具集锦--Awk―sed―cut(row-based, column-based),find、grep、xargs、sort、uniq、tr、cut、paste、wc

    本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...

  7. Linux Shell 文本处理工具集锦 zz

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

  8. Linux Shell 文本处理工具集锦

    本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...

  9. [转] Linux Shell 文本处理工具集锦

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

随机推荐

  1. zeptojs库解读1之整体框架

    首先看的是整体框架, // zepto骨骼,这个函数的作用使得Zepto(slector, context)使用很多$.fn里面的方法 var Zepto = (function(){ // zept ...

  2. lvs+keepalived+bind实现负载均衡高可用智能dns【转】

    转:https://www.cnblogs.com/mikeluwen/p/7068356.html 整体架构: 1.IP地址规划: Dns1:172.28.0.54 Dns2:172.28.0.55 ...

  3. 【hive】分组求排名

    分组求排名 相信好多使用Mysql的用户一定对分组求排名的需求感到发怵. 但是在hive或者oracle来说就能简单实现. 采用窗口函数:rank() over() / row_number() ov ...

  4. input type="file"在各个浏览器下的默认样式,以及修改自定义样式

    一.<input type="file"/>在各个浏览器中的默认样式: 系统 浏览器 样式效果 点击效果 mac google 点击按钮和输入框都可以打开文件夹 mac ...

  5. Hadoop1.x HDFS系统架构

    1. HDFS中的一些概念1.1 数据块1.2 NameNode和DataNode1.2.1 管理者:Namenode1.2.1 工作者:Datanode1.3 Secondary Namenode1 ...

  6. Scrum立会报告+燃尽图 06

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289] 版本控制:https://git.coding.net/liuyy08 ...

  7. 浅谈ES6的let和const的异同点

    1.let和const的相同点: ① 只在声明所在的块级作用域内有效. ② 不提升,同时存在暂时性死区,只能在声明的位置后面使用. ③ 不可重复声明. 2.let和const的不同点: ① let声明 ...

  8. C++ wait捕捉的信号处理WIFEXITED/WEXITSTATUS/WIFSIGNALED

    当一个进程正常或异常终止的时候,内核就像其父进程发送SIGCHLD信号,因为子进程是个异步事件,所以这种信号也是内核给那个父进程发的异步通知.父进程可以选择忽略该信号,或者提供一个该信号发生时即被调用 ...

  9. Jmeter-Logic Controllers(逻辑控制器)

    Critical Section Controller(临界区控制器) 参考:http://www.cnblogs.com/yanzhe/p/7729984.html ForEach Controll ...

  10. BZOJ1718: [Usaco2006 Jan] Redundant Paths 分离的路径【边双模板】【傻逼题】

    LINK 经典傻逼套路 就是把所有边双缩点之后叶子节点的个数 //Author: dream_maker #include<bits/stdc++.h> using namespace s ...