ChinaUnix上大神網中人总结的Shell十三问?,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点。每一问都写得非常精彩。 同样是ChinaUnix上,wingger大神整理的Shell基础二十篇。这份文档涉及的内容比较多,我没记错的话应该有很多命令的用法,而且配备实际使用的例子,对初学者帮助甚大。 Shell脚本专家指南也是一本不可多得的好书,我是之前在学校的时候买的这本书。上面都是作者实际工作中的经验总结,你可以从中学到很多其它书上学不到的实践知识。千万不要被专家两个字吓住了,书名的意思是专家给你总结的学习指南。 注:mingxinglai也在他的博客文章shell脚本学习材料种整理了很多实用的资料。 正则表达式 我个人学习正则表达式是从正则表达式30分钟入门教程开始的,这份文档让我了解了正则表达式的一些基础术语和基本的用法。 Linux下的正则表达式学习相对资源比较丰富,但同样得,因为不同工具的正则表达式或多或少都有一些不同,所以初学起来也比较让人困扰,但是正则表达式的关键在于多学多用。多看看grep/sed/awk中正则的用法,慢慢得就熟悉了。 这里推荐一篇InfoQ上介绍Linux下正则表达式不同流派之前的区别的文章——Linux/Unix工具与正则表达式的POSIX规范,读了它之后你会让你有种豁然开朗的感觉。 Sed与awk…
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