linux中sed的用法详解(对行数据的添加、删除等)

sed使用语法

[[email protected] test]# sed --help

用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...

-n, --quiet, --silent                 取消自动打印模式空间

-e 脚本, --expression=脚本                 添加“脚本”到程序的运行列表

-f 脚本文件, --file=脚本文件                 添加“脚本文件”到程序的运行列表

--follow-symlinks                 follow symlinks when processing in place; hard links will still be broken.

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied).

The default operation mode is to break symbolic and hard links.

This can be changed with --follow-symlinks and --copy.

-c, --copy

use copy instead of rename when shuffling files in -i mode.

While this will avoid breaking links (symbolic or hard), the

resulting editing operation is not atomic.  This is rarely

the desired mode; --follow-symlinks is usually enough, and

it is both faster and more secure.

-l N, --line-length=N                 指定“l”命令的换行期望长度

--posix                 关闭所有 GNU 扩展

-r, --regexp-extended                 在脚本中使用扩展正则表达式

-s, --separate                 将输入文件视为各个独立的文件而不是一个长的连续输入

-u, --unbuffered                 从输入文件读取最少的数据,更频繁的刷新输出

--help     打印帮助并退出

--version  输出版本信息并退出

打印出行号,并删除2-5行

[[email protected] test]# nl /etc/passwd | sed ‘2,5d‘ |more

    1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

6  sync:x:5:0:sync:/sbin:/bin/sync

7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8  halt:x:7:0:halt:/sbin:/sbin/halt

9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

打印出行号,只删除2-5行

[[email protected] test]# nl /etc/passwd | sed ‘2d‘ |more

1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

3  daemon:x:2:2:daemon:/sbin:/sbin/nologin

4  adm:x:3:4:adm:/var/adm:/sbin/nologin

5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

打印出行号,删除第2行以后所有内容

[[email protected] test]# nl /etc/passwd | sed ‘2,$d‘

1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

[[email protected] test]#

打印出行号,在第2行后加上“drink tea”

[[email protected] test]# nl /etc/passwd |sed ‘2a drink tea‘ | more

1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

2  bin:x:1:1:bin:/bin:/sbin/nologin

drink tea

     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin

4  adm:x:3:4:adm:/var/adm:/sbin/nologin

打印出行号,加入2行“drink tea or drink beer”

[[email protected] test]# nl /etc/passwd |sed ‘2a drink tea or ...\

drink beer?‘ | more

1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

2  bin:x:1:1:bin:/bin:/sbin/nologin

drink tea or ...

drink beer?

3  daemon:x:2:2:daemon:/sbin:/sbin/nologin

4  adm:x:3:4:adm:/var/adm:/sbin/nologin

打印出行号,将第2-5行的内容更换成“No 2-5”

[[email protected] test]# nl /etc/passwd | sed ‘2,5c No 2-5 number‘ | more

     1  root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash

No 2-5 number

6  sync:x:5:0:sync:/sbin:/bin/sync

7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8  halt:x:7:0:halt:/sbin:/sbin/halt

打印出行号,仅列出第5-7行的内容

[[email protected] test]# nl /etc/passwd | sed -n ‘5,7p‘

     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6  sync:x:5:0:sync:/sbin:/bin/sync

7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

时间: 2024-12-10 00:24:08

linux中sed的用法详解(对行数据的添加、删除等)的相关文章

(转)linux 中特殊符号用法详解

linux 中特殊符号用法详解 原文:https://www.cnblogs.com/lidabo/p/4323979.html # 井号 (comments)#管理员  $普通用户 脚本中 #!/bin/bash   #!/bin/sh井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行. # This line is comments.echo "a = $a" # a = 0由于这个特性,当临时不想执行某行指令时,只需在该行开头加上 # 就

linux 中特殊符号用法详解

# 井号 (comments)#管理员  $普通用户 脚本中 #!/bin/bash   #!/bin/sh井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注解文字,不会被执行. # This line is comments.echo "a = $a" # a = 0由于这个特性,当临时不想执行某行指令时,只需在该行开头加上 # 就行了.这常用在撰写过程中.#echo "a = $a" # a = 0如果被用在指令中,或者引号双引号括住的话

linux中的tr用法详解

tr命令可以对来自标准输入的字符进行替换.压缩和删除.它可以将一组字符成另一组字符,经常用来编写优美的单行命令,作用很强大. tr(选项)(参数) -c或--complerment:取代所有不属于第一字符集的字符:  -d或--delete:删除所有属于第一字符集的字符: -s或--squeeze-repeats:把连续重复的字符以单独一个字符表示: -t或--truncate-set1:先删除第一字符集较变第二字符集多出的字符. 将输入字符由大写转换为小写: echo "HELLO W[obj

(转)Linux命令之Ethtool用法详解

Linux命令之Ethtool用法详解 原文:http://www.linuxidc.com/Linux/2012-01/52669.htm Linux/Unix命令之Ethtool描述:Ethtool是用于查询及设置网卡参数的命令.概要:ethtool ethX      //查询ethX网口基本设置ethtool –h        //显示ethtool的命令帮助(help)ethtool –i ethX    //查询ethX网口的相关信息 ethtool –d ethX    //查询

PHP中的ob_start用法详解

用PHP的ob_start();控制您的浏览器cache Output Control 函数可以让你自由控制脚本中数据的输出.它非常地有用,特别是对于:当你想在数据已经输出后,再输出文件头的情况.输出控制函数不对使用 header() 或 setcookie(), 发送的文件头信息产生影响,只对那些类似于 echo() 和 PHP 代码的数据块有作用.我们先举一个简单的例子,让大家对Output Control有一个大致的印象:Example 1. 程序代码 程序代码<?phpob_start(

c++中vector的用法详解

c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include<vector>以包含所需要的类文件vector 还有一定要加上using namespace std; 2.变量声明: 2.1 例:声明一个int向量以替代一维的数组:vector <int> a;(等于声明了一个

hibenate中genarator的用法详解

ARM IDE 使用 ADS(ARM Developer Suite),是在1993年由Metrowerks公司开发是ARM处理器下最主要的开发工具. 新建工程:exp-2 新建文件exp-2.s到工程exp-2中: 创建好后,编辑源代码. 调试设置:AXD->options->configure Target..,选armul: ARMlinker中设置 RO Base与RW Base与Image Entry Point: 开始调试.add r1,r1,r2 // 将r1+r2的结果存储到r

(转)Shell中read的用法详解

Shell中read的用法详解 原文:http://blog.csdn.net/jerry_1126/article/details/77406500 read的常用用法如下: read -[pstnd] var1 var2 ... -p 提示语句-n 字符个数-s 屏蔽回显-t 等待时间-d 输入分界 [plain] view plain copy 01). read                           # 从标准输入读取一行并赋值给特定变量REPLY [email prote

Python中self的用法详解,或者总是提示:TypeError: add() missing 1 required positional argument: &#39;self&#39;的问题解决

https://blog.csdn.net/songlh1234/article/details/83587086 下面总结一下self的用法详解,大家可以访问,可以针对平时踩过的坑更深入的了解下. https://blog.csdn.net/CLHugh/article/details/75000104, Python中self的用法详解,或者总是提示:TypeError: add() missing 1 required positional argument: 'self'的问题解决 原文