美文网首页
文本处理 sed

文本处理 sed

作者: 7f0a92cda77c | 来源:发表于2021-06-07 19:52 被阅读0次

sed 章

sed:流编辑器,一般用来对文本进行增删改查
用法:sed [-options] script files
常见参数:

-n: 取消默认输出,只显示经过sed处理或匹配的行(常用)

-e:直接在命令模式上进行sed的动作编辑,接 要执行的一个或者多个命令

-i:直接修改读取的文件内容,不输出

script 主要是下面的几个

[address][!][command]
常见script address:
address Explanation
2 第2行
2,4 第2行 到 第4行
2,$ 第2行 到 最后一行
2~3 从第二行开始, 每隔 3 行 取一行
2, +4 从第2行 到 2+4 行
/pattern/ 匹配上pattern 的行
[!] 表示否定,取反
常见script command
command Explanation
a append-在指定行的后增加一行
i insert-在指定行的前增加一行
d delete-删除某一行或者某几行
c change-改变指定行的内容
s 替换,使用格式为's/pattern/new/[flags]'
y 转换,实现字符一对一转换,格式为'y/inchars/outchars'
p print-把匹配或修改过的行打印出来,通常与-n参数合用

With no flags, the first matched substitution is changed. With the "g" option, all matches are changed. If you want to modify a particular pattern that is not the first one on the line, you could use "(" and ")" to mark each pattern, and use "\1" to put the first pattern back unchanged.

each line 每一行的操作
输出结果
cat readme.txt |sed '1,3i WWWWWWWWWWWWWWWWWWW'
WWWWWWWWWWWWWWWWWWW
###this is the practice text
WWWWWWWWWWWWWWWWWWW
Welcome to this world
WWWWWWWWWWWWWWWWWWW
The Linux world is fascinating and amazing!
You will get the regular expression practice
word
wolf
would
wood
woooo
wo
woooooooooooo
And these are the necessary path to the Linux world!
cat readme.txt |sed '2,9c ***********'
###this is the practice text
***********
wo
woooooooooooo
And these are the necessary path to the Linux world!
替换大小写

方式1

cat readme.txt |tr '[a-z]' '[A-Z]'

方式2

cat readme.txt |sed 's/[A-Z]/\l&/g'#大写字母替换成小写字母
cat readme.txt |sed 's/[a-z]/\u&/g'#小写字母替换成大写字母

替换前几个字符串

sed 's/^../QQ/g' readme.txt #替换前2个字符串
sed 's/^.\{10\}/QQ/g' readme.txt  #替换前10个字符串</pre>

奇数行和偶数行操作

sed -n '1~2p' readme.txt ###对奇数行打印
sed -n '2~2p' readme.txt ###对偶数行打印</pre>
替换空白
head cas12.txt|sed   's#\s$##'|cat -A

#A comment, until the next newline.

s-表示空格

S-表示非空格

将所有行合并成为一行
sed ':a;N;s/\n//;t a;' SARS_2 |wc
less SARS_2 | awk BEGIN{RS=EOF}'{gsub(/\n/,"");print}'|less -SN
说明::a 在代码开始处设置一个标记a,在代码执行到结尾处时利用跳转命令t a重新跳转到标号a处,重新执行代码,这样就可以递归的将所有行合并成一行;N 命令,将下一行读入并附加到当前行后面。
awk默认将记录分隔符(record separator即RS)设置为\n,此行代码将RS设置为EOF(文件结束),也就是把文件视为一个记录,然后通过gsub函数将\n替换成空格,最后输出。
没有合并的
合并后变成一行

所有学习的记录笔记都是源于生信菜鸟团笔记
https://www.cnblogs.com/zhanzhan/p/9016550.html

相关文章

  • sed命令基本用法

    sed文本处理工具的用法: 用法1:前置命令 | sed [选项] '条件指令'用法2:sed [选项] ...

  • sed基础命令

    一、sed文本处理工具的用法: 用法1:前置命令 | sed [选项] '条件指令' 用法2:sed [选...

  • inux学习 Day15-sed基本用法

    文本处理工具:grep,sed(流编辑器),awk sed基本用法:sed(Stream EDitor)行编辑器(...

  • linux三剑客--sed

    功能 自动处理文件 分析日志文件 修改配置文件 sed如何进行文本处理的?sed处理过程.png 格式 $sed ...

  • Linux文本处理三剑客——awk

    文本处理三工具:grep,sed,awk grep:文本过滤工具:pattern; sed:行编辑器:模式空间、保...

  • Linux文本处理三剑客——sed

    文本处理三工具:grep,sed,awk grep:文本过滤工具:pattern sed:行编辑器:模式空间、保持...

  • Linux文本处理三剑客——grep

    文本处理三工具:grep,sed,awk grep:文本过滤工具:pattern sed:行编辑器:模式空间、保持...

  • 如何使用 sed 命令删除文件中的行

    Sed 代表流编辑器(Stream Editor),常用于 Linux 中基本的文本处理。sed 命令是 Linu...

  • sed

    文本处理sed sed(Stream EDitor, 行编辑器):处理文本的工具。sed是一种流编辑器,它一次处...

  • sed的认识和基本应用

    1.Sed 介绍 sed全名叫stream deitor,流编辑器。作为shell文本处理三剑客之一的sed,其不...

网友评论

      本文标题:文本处理 sed

      本文链接:https://www.haomeiwen.com/subject/gymjeltx.html