Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number

 

做一个文本文件做测试:

Given below is content of file called test. Absolute path is /tmp/test

This is a test
for sed command
to be performed
on test server
Hello World
How do you do

本教程使用两种模式:We are using two methods in this tutorial

模式一: 使用‘d’ 删除 ,Method 1 : By using ‘d’ command i.e for delete
模式二: 使用‘p’ 打印, Method 2 : By using ‘p’ command i.e for print

 

SED : 打印某一行 Using ‘d’ command for printing particular line number

sed '4!d' /tmp/test

Below is the output :

sharad@linuxworld:~$ sed '4!d' /tmp/test 
on test server
sharad@linuxworld:~$

Syntax:
N = 行号 Line number
!d = 不删除 Do not delete

 

打印行号区间 Using ‘d’ command to print range of line number by sed command

You can also define, the range of line number. For example, we want to print from line number 2 to 4.

sed '2,4 !d' /tmp/test

Below is the output :

sharad@linuxworld:~$ sed '2,4 !d' /tmp/test 
for sed command
to be performed
on test server
sharad@linuxworld:~$

Syntax:
NS = 开始行号 From Line number
NT = 结束行号 To Line number
!d = 不删除 Do not delete

sed 'NF,NT !d' /path/of/file

 

SED : 打印 Using ‘p’ command for printing particular line number

sed -n '3p' /tmp/test

Below given is the output:

sharad@linuxworld:~$ sed -n '3p' /tmp/test 
to be performed
sharad@linuxworld:~$

Syntax:
-n = 不打印,除非有打印的命令 Nothing will print unless an explicit request to print is found.
N = 行号 Line number
p = 打印 print

sed -n 'Np' /path/of/file

 

打印区间 Using ‘p’ command to print range of line number by sed command

In this section, we will print range of line numbers by using ‘p’ command with sed.
Here, we are printing from line number 2 to 5 .

sed -n '2,5p' /tmp/test

Below given is the output:

sharad@linuxworld:~$ sed -n '2,5p' /tmp/test 
for sed command
to be performed
on test server
Hello World
sharad@linuxworld:~$

 

Syntax:
-n = 不打印,除非有打印命令 Nothing will print unless an explicit request to print is found.
NF = 开始行号 From Line number
NT = 结束行号 To Line number
p = 打印 print

 

再一个实例:

文本文件 threads.txt

1249
1254
1260
1267
1275
1289
1298

搜索打印 1275 所在

$sed -n "/1275/p" threads.txt
1275

或者

$ sed -n "/12/p" threads.txt
1249
1254
1260
1267
1275
1289
1298

打印第二行

$ sed '2!d' threads.txt
1249

删除第一行的空行,并保存结果

$ sed -ie '1d' threads.txt
1249
1254
1260
1267
1275
1289
1298

打印1275所在行数

$ sed -n "/1275/=" threads.txt
5

使用变量:

$ export threads=$(sed -n "/12/=" threads_cusomters.txt)

$ echo $threads
1 2 3 4 5 6 7

$ echo "$threads"
1
2
3
4
5
6
7
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number

 

Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number
Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number

 

 

本文:Shell: sed 获取匹配串的行号, sed删除某一行, sed打印某一行, sed print particular line number

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.