编程小贴士

给你的编程提供小点子


Linux下常用文本处理命令

linux下面有很多经典的非常有用的命令,其中处理文本的命令就有很多。这些小工具经过了几十年时间的洗礼,现在已经变成了经典,已经变成了Linux下面的标准,其实它们一直是遵循着Linux的标准。下面就让我们一起看看这些经典的Linux文本处理命令。

一. sort

文件排序, 通常用在管道中当过滤器来使用. 这个命令可以依据指定的关键字或指定的字符位置, 对文件行进行排序. 使用-m选项, 它将会合并预排序的输入文件. 想了解这个命令的全部参数请参考这个命令的info页.

 

二. tsort

拓扑排序, 读取以空格分隔的有序对, 并且依靠输入模式进行排序.

三. uniq

这个过滤器将会删除一个已排序文件中的重复行. 这个命令经常出现在sort命令的管道后边.

四. expand, unexpand

expand命令将会把每个tab转化为一个空格. 这个命令经常用在管道中.

unexpand命令将会把每个空格转化为一个tab. 效果与expand命令相反.

五. cut

一个从文件中提取特定域的工具. 这个命令与awk中使用的print $N命令很相似, 但是更受限. 在脚本中使用cut命令会比使用awk命令来得容易一些. 最重要的选项就是-d(字段定界符)和-f(域分隔符)选项.

六. paste

将多个文件, 以每个文件一列的形式合并到一个文件中, 合并后文件中的每一列就是原来的一个文件. 与cut结合使用, 经常用于创建系统log文件.

七. join

这个命令与paste命令属于同类命令. 但是它能够完成某些特殊的目地. 这个强力工具能够以一种特殊的形式来合并两个文件, 这种特殊的形式本质上就是一个关联数据库的简单版本.

join命令只能够操作两个文件. 它可以将那些具有特定标记域(通常是一个数字标签)的行合并起来, 并且将结果输出到stdout. 被加入的文件应该事先根据标记域进行排序以便于能够正确的匹配.

八. head

把文件的头部内容打印到stdout上(默认为10行, 可以自己修改). 这个命令有一些比较有趣的选项.

九. tail

将一个文件结尾部分的内容输出到stdout中(默认为10行). 通常用来跟踪一个系统logfile的修改情况, 如果使用-f选项的话, 这个命令将会继续显示添加到文件中的行.

十. wc

wc可以统计文件或I/O流中的”单词数量”:

十一. fold

将输入按照指定宽度进行折行. 这里有一个非常有用的选项-s, 这个选项可以使用空格进行断行(译者: 事实上只有外文才需要使用空格断行, 中文是不需要的)(请参考例子 12-23和例子 A-1).

十二. fmt

一个简单的文件格式器, 通常用在管道中, 将一个比较长的文本行输出进行”折行”.

十三. col

这个命令用来滤除标准输入的反向换行符号. 这个工具还可以将空白用等价的tab来替换. col工具最主要的应用还是从特定的文本处理工具中过滤输出, 比如groff和tbl. (译者: 主要用来将man页转化为文本.)

十四. column

列格式化工具. 通过在合适的位置插入tab, 这个过滤工具会将列类型的文本转化为”易于打印”的表格式进行输出.

十五. colrm

列删除过滤器. 这个工具将会从文件中删除指定的列(列中的字符串)并且写到文件中, 如果指定的列不存在, 那么就回到stdout. colrm 2 4 <filename将会删除filename文件中每行的第2到第4列之间的所有字符.

Caution: 如果这个文件包含tab和不可打印字符, 那将会引起不可预期的行为. 在这种情况下, 应该通过管道的手段使用expand和unexpand来预处理colrm.

十六. nl

计算行号过滤器. nl filename将会把filename文件的所有内容都输出到stdout上, 但是会在每个非空行的前面加上连续的行号. 如果没有filename参数, 那么就操作stdin.

nl命令的输出与cat -n非常相似, 然而, 默认情况下nl不会列出空行.

十七. pr

格式化打印过滤器. 这个命令会将文件(或stdout)分页, 将它们分成合适的小块以便于硬拷贝打印或者在屏幕上浏览. 使用这个命令的不同的参数可以完成好多任务, 比如对行和列的操作, 加入行, 设置页边, 计算行号, 添加页眉, 合并文件等等. pr命令集合了许多命令的功能, 比如nl, paste, fold, column, 和expand.

pr -o 5 –width=65 fileZZZ | more 这个命令对fileZZZ进行了比较好的分页, 并且打印到屏幕上. 文件的缩进被设置为5, 总宽度设置为65.

一个非常有用的选项-d, 强制隔行打印(与sed -G效果相同).

十八. gettext

GNU gettext包是专门用来将程序的输出翻译或者本地化为不同国家语言的工具集. 在最开始的时候仅仅支持C语言, 现在已经支持了相当数量的其它程序语言和脚本语言.

想要查看gettext程序如何在shell脚本中使用. 请参考info页.

十九. msgfmt

一个产生二进制消息目录的程序. 这个命令主要用来本地化.

二十. iconv

一个可以将文件转化为不同编码格式(字符集)的工具. 这个命令主要用来本地化.

二十一. recode

可以认为这个命令是上边iconv命令的专业版本. 这个非常灵活的并可以把整个文件都转换为不同编码格式的工具并不是Linux标准安装的一部分.

二十二. TeX, gs

TeX和Postscript都是文本标记语言, 用来对打印和格式化的视频显示进行预拷贝.

TeX是Donald Knuth精心制作的排版系统. 通常情况下, 通过编写脚本的手段来把所有的选项和参数封装起来一起传到标记语言中是一件很方便的事情.

Ghostscript (gs) 是一个 遵循GPL的Postscript解释器.

二十三. enscript

将纯文本文件转换为PostScript的工具

比如, enscript filename.txt -p filename.ps 产生一个 PostScript 输出文件filename.ps.

二十四. groff, tbl, eqn

另一种文本标记和显示格式化语言是groff. 这是一个对传统UNIX roff/troff显示和排版包的GNU增强版本. Man页使用的就是groff.

tbl表处理工具可以认为是groff的一部分, 它的功能就是将表标记转化到groff命令中.

eqn等式处理工具也是groff的一部分, 它的功能是将等式标记转化到groff命令中.

二十五. lex, yacc

lex是用于模式匹配的词汇分析产生程序. 在Linux系统上这个命令已经被flex取代了.

yacc工具基于一系列的语法规范, 产生一个语法分析器. 在Linux系统上这个命令已经被bison取代了.

二十六. tr

字符转换过滤器.

120 Responses to “ Linux下常用文本处理命令 ”

  1. i love pron说道:

    bbtecN this is now one awesome article. Really pumped up about read more. undoubtedly read onaаАа’б‚Т€ТšаЂаŒаАТ’аЂа†

  2. I truly appreciate this blog article.Thanks Again. Really Great.

  3. kiss movies说道:

    Really enjoyed this post.Thanks Again. Awesome.

  4. animal coloring说道:

    The majority of of the commentary on this web site dont make sense.

  5. This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your excellent post. Also, I ave shared your website in my social networks!

  6. Lung Detox说道:

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I have shared your web site in my social networks!

  7. This page definitely has all of the information I needed concerning this subject and didn at know who to ask.

  8. Thanks again for the blog post.Really thank you! Awesome.

  9. learn more说道:

    There is definately a lot to find out about this issue. I really like all the points you made.

  10. You can not imagine simply how much time I had spent for this information!

  11. Oluwadamilare说道:

    Regards for helping out, excellent information.

  12. This very blog is no doubt educating additionally informative. I have picked helluva helpful advices out of it. I ad love to return again soon. Thanks a lot!

  13. cialis pills说道:

    I was able to find good info from your content.

  14. I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thanks again

  15. buy说道:

    Thanks so much for the blog article.Thanks Again. Keep writing.

  16. It is truly a great and useful piece of info. I am happy that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

  17. I truly appreciate this article.Really looking forward to read more. Really Cool.

  18. Wow, great blog post.Thanks Again. Will read on

  19. online casino bonus view of Three Gorges | Wonder Travel Blog

  20. Airport taxi说道:

    This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Many thanks!

  21. Thanks for sharing, this is a fantastic post.Thanks Again. Cool.

  22. status online说道:

    This is really interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your excellent post. Also, I have shared your site in my social networks!

  23. browse说道:

    I’а†ve learn some just right stuff here. Definitely price bookmarking for revisiting. I wonder how much effort you set to make the sort of great informative website.

  24. Thank you, I have recently been looking for info about this subject for ages and yours is the best I ave discovered till now. But, what about the bottom line? Are you sure about the source?

  25. information with us. Please stay us up to date like this.

  26. Major thankies for the article. Awesome.

  27. Nwokolo Collins说道:

    Very informative blog post. Will read on

  28. check that说道:

    Very good write-up. I absolutely appreciate this website. Thanks!

  29. Nwokolo说道:

    Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is excellent, let alone the content!

  30. cay thong noel说道:

    Wow! This could be one particular of the most helpful blogs We ave ever arrive across on this subject. Actually Great. I am also a specialist in this topic therefore I can understand your hard work.

  31. Really informative blog post.Thanks Again. Great.

  32. Thank you for your blog post.Really thank you! Awesome.

  33. trash cans说道:

    It as actually a cool and useful piece of information. I am glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  34. Drake Mp3说道:

    The right way to Clean A person as Lcd Television

  35. We stumbled over here different web page and thought I might as well check things out. I like what I see so now i am following you. Look forward to checking out your web page for a second time.

  36. visit说道:

    Looking forward to reading more. Great blog.Really looking forward to read more. Really Great.

  37. to find something more safe. Do you have any suggestions?

  38. casino online说道:

    Say, you got a nice blog article.Thanks Again. Awesome.

  39. I value the blog article.Really looking forward to read more. Cool.

  40. Informative article, exactly what I needed.

  41. Supermodel Elena说道:

    Right away I am going to do my breakfast, after having my breakfast coming yet again to read more news.

  42. this website说道:

    Thanks-a-mundo for the post.Much thanks again. Great.

  43. this website说道:

    It as difficult to find knowledgeable people in this particular subject, however, you seem like you know what you are talking about! Thanks

  44. rating说道:

    This particular blog is obviously entertaining as well as informative. I have found a lot of interesting things out of this blog. I ad love to go back every once in a while. Cheers!

  45. fashion说道:

    I will immediately clutch your rss feed as I can at find your email subscription link or newsletter service. Do you have any? Kindly permit me recognize in order that I may just subscribe. Thanks.

  46. I really liked your blog post.Really looking forward to read more. Keep writing.

  47. There as certainly a lot to learn about this issue. I like all of the points you ave made.

  48. Keep up the good work , I read few blog posts on this web site and I believe that your web blog is real interesting and holds circles of fantastic info.

  49. written by him as nobody else know such detailed about my difficulty.

  50. Tejido de punto说道:

    It as hard to find experienced people in this particular subject, but you sound like you know what you are talking about! Thanks

  51. Crochet basico说道:

    This website was how do you say it? Relevant!! Finally I have found something that helped me. Thank you!

  52. local说道:

    Thanks for sharing, this is a fantastic blog article.Really thank you! Awesome.

  53. blue nike说道:

    Remarkable! Its actually amazing paragraph, I have got much clear idea concerning from this article.

  54. value说道:

    Would you be involved in exchanging links?

  55. I value the blog article.Really looking forward to read more. Really Great.

  56. Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, as well as the content!

  57. There is clearly a bunch to realize about this. I think you made certain nice points in features also.

  58. Waec expo说道:

    Valuable info. Lucky me I found your web site by accident, and I am shocked why this accident didn at happened earlier! I bookmarked it.

  59. School admission说道:

    It is in fact a cool and beneficial piece of details. I am content which you just shared this practical information and facts with us. Please retain us informed like this. Thank you for sharing.

  60. Well I definitely enjoyed studying it. This information offered by you is very useful for proper planning.

  61. This is one awesome article.Much thanks again. Really Great.

  62. I really liked your article.Really looking forward to read more. Keep writing.

  63. There is certainly a lot to find out about this subject. I love all of the points you ave made.

  64. pretty beneficial material, overall I believe this is worthy of a bookmark, thanks

  65. Open heaven说道:

    ItaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžs actually a nice and useful piece of info. I am glad that you shared this useful information with us. Please keep us informed like this. Thanks for sharing.

  66. Glennis说道:

    Wow, great post.Thanks Again. Fantastic.

  67. I think this is one of the most vital info for me. And i am glad reading your article. But should remark on some general things, The website

  68. Very neat article.Really thank you! Much obliged.

  69. scary maze说道:

    I think other site proprietors should take this website as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic!

  70. It as hard to find knowledgeable people about this topic, but you

  71. With havin so much content do you ever run into any problems of plagorism or copyright infringement?

  72. Wow, great blog article.Really looking forward to read more. Awesome.

  73. tips de ventas说道:

    Thanks for the article post.Much thanks again. Will read on

  74. Real estate说道:

    This website truly has all the information I wanted about this subject and didn at know who to ask.

  75. Meshack说道:

    Spot on with this write-up, I really believe this website needs much more attention. I all probably be returning to see more, thanks for the information!

  76. I will right away grasp your rss feed as I can at in finding your email subscription hyperlink or newsletter service. Do you have any? Kindly permit me recognize in order that I may subscribe. Thanks.

  77. traveler说道:

    Really appreciate you sharing this blog article.Thanks Again. Cool.

  78. softfind说道:

    Thanks for taking the time to talk about this, I feel fervently about this and I take pleasure in learning about this topic. Please, as you gain

  79. Thanks for sharing, this is a fantastic blog post.Thanks Again. Want more.

  80. This is a topic which is close to my heart Best wishes! Where are your contact details though?

  81. Practical goal rattling great with English on the other hand find this rattling leisurely to translate.

  82. Thanks again for the post.Much thanks again. Really Great.

  83. pool builders说道:

    You have mentioned very interesting points ! ps nice website.

  84. personal injury说道:

    Whoa! This blog looks just like my old one! It as on a entirely different subject but it has pretty much the same layout and design. Wonderful choice of colors!

  85. Very rapidly this web site will be famous amid all blogging

  86. Wow, wonderful weblog structure! How long have you ever been running a blog for? you made blogging glance easy. The overall look of your website is magnificent, let alone the content material!

  87. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!

  88. Chapel说道:

    Thankyou for this terrific post, I am glad I discovered this website on yahoo.

  89. go to说道:

    This unique blog is obviously educating and also amusing. I have picked up many helpful tips out of this blog. I ad love to visit it over and over again. Thanks!

  90. I truly appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You ave made my day! Thank you again!

  91. wow, superb blog post.Really pumped up about read more. Really want more.

  92. visit website说道:

    Your style is unique in comparison to other folks I ave read stuff from. Thank you for posting when you ave got the opportunity, Guess I all just bookmark this web site.

  93. click here说道:

    When some one searches for his necessary thing, therefore he/she needs to be available that in detail, therefore that thing is maintained over here.

  94. Major thankies for the blog post.Thanks Again. Great.

  95. story. I was surprised you aren at more popular given that you definitely possess the gift.

  96. Im obliged for the blog.Really thank you! Keep writing.

  97. post说道:

    I value the post.Much thanks again. Awesome.

  98. ndu说道:

    Really informative blog article.Thanks Again. Want more.

  99. This excellent website certainly has all of the info I needed concerning this subject and didn at know who to ask.

  100. announce online说道:

    This very blog is without a doubt entertaining as well as amusing. I have found a lot of handy stuff out of this blog. I ad love to go back over and over again. Cheers!

  101. Not clear on what you have in mind, Laila. Can you give us some more information?

  102. check out说道:

    I truly appreciate this article post.Much thanks again. Cool.

  103. Thanks-a-mundo for the blog post.Really looking forward to read more. Cool.

  104. Wow, great article.Really looking forward to read more. Keep writing.

  105. Health Host说道:

    Wow, wonderful weblog format! How long have you been blogging for? you make running a blog look easy. The total look of your website is wonderful, let alone the content material!

  106. Really informative article post.Thanks Again. Really Cool.

  107. ballinaadvocate说道:

    you are in point of fact a good webmaster. The site loading speed is incredible.

  108. visit说道:

    Really enjoyed this blog post.Much thanks again. Fantastic.

  109. Looking around I like to look around the web, regularly I will go to Digg and follow thru

  110. tu mat alaska说道:

    This awesome blog is really interesting and informative. I have discovered a lot of interesting stuff out of this amazing blog. I ad love to visit it every once in a while. Thanks a lot!

  111. official site说道:

    You made some clear points there. I did a search on the issue and found most individuals will go along with with your website.

  112. I reckon something truly special in this website.

  113. blog saya说道:

    These are in fact wonderful ideas in regarding blogging.

  114. when it comes when it comes to tv fashion shows, i really love Project Runway because it shows some new talents in the fashion industry**

  115. Muchos Gracias for your article.Really looking forward to read more. Will read on

  116. genuine parts说道:

    You are my aspiration, I possess few blogs and rarely run out from brand .

  117. good day说道:

    Really enjoyed this article post.Thanks Again. Will read on

  118. Really appreciate you sharing this blog.Much thanks again. Cool.

  119. Would appreciate to constantly get updated great blog !.

  120. learn more说道:

    You are my function designs. Many thanks for that post

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>