目录
当前位置: 首页 > 文档资料 > Shell 中文文档 >

Appendix C. A Sed and Awk Micro-Primer

优质
小牛编辑
123浏览
2023-12-01

This is a very brief introduction to the sedand awktext processing utilities. We willdeal with only a few basic commands here, but that will sufficefor understanding simple sed and awk constructs within shellscripts.

sed: a non-interactive text file editor

awk: a field-oriented pattern processinglanguage with a C-like syntax

For all their differences, the two utilities share a similarinvocation syntax, both use regularexpressions , both read input by defaultfrom stdin, and both output tostdout. These are well-behaved UNIX tools,and they work together well. The output from one can be pipedinto the other, and their combined capabilities give shellscripts some of the power of Perl.

One important difference between the utilities isthat while shell scripts can easily pass arguments to sed, itis more complicated for awk (see Example 33-5and Example 9-23).

Table C-1. Basic sed operators

OperatorNameEffect
[address-range]/pprintPrint [specified address range]
[address-range]/ddeleteDelete [specified address range]
s/pattern1/pattern2/substituteSubstitute pattern2 for first instance of pattern1 in a line
[address-range]/s/pattern1/pattern2/substituteSubstitute pattern2 for first instance of pattern1 in aline, over address-range
[address-range]/y/pattern1/pattern2/transformreplace any character in pattern1 with thecorresponding character in pattern2, overaddress-range(equivalent oftr)
gglobalOperate on everypattern matchwithin each matched line of input

Unless the g(global) operator is appended to asubstitutecommand, the substitutionoperates only on the first instance of a pattern match withineach line.

From the command line and in a shell script, a sed operation mayrequire quoting and certain options.

   1 sed -e '/^$/d' $filename
   2 # The -e option causes the next string to be interpreted as an editing instruction.
   3 #  (If passing only a single instruction to "sed", the "-e" is optional.)
   4 #  The "strong" quotes ('') protect the RE characters in the instruction
   5 #+ from reinterpretation as special characters by the body of the script.
   6 # (This reserves RE expansion of the instruction for sed.)
   7 #
   8 # Operates on the text contained in file $filename.

In certain cases, a sedediting command willnot work with single quotes.

   1 filename=file1.txt
   2 pattern=BEGIN
   3
   4   sed "/^$pattern/d" "$filename"  # Works as specified.
   5 # sed '/^$pattern/d' "$filename"    has unexpected results.
   6 #        In this instance, with strong quoting (' ... '),
   7 #+      "$pattern" will not expand to "BEGIN".

Sed uses the -eoptionto specify that the following string is an instruction or setof instructions. If there is only a single instruction containedin the string, then this option may be omitted.

   1 sed -n '/xzy/p' $filename
   2 # The -n option tells sed to print only those lines matching the pattern.
   3 # Otherwise all input lines would print.
   4 # The -e option not necessary here since there is only a single editing instruction.

Table C-2. Examples of sed operators

NotationEffect
8dDelete 8th line of input.
/^$/dDelete all blank lines.
1,/^$/dDelete from beginning of input up to, and includingfirst blank line.
/Jones/pPrint only lines containing "Jones"(with

-noption).s/Windows/Linux/Substitute "Linux"for first instanceof "Windows"found in each input line.s/BSOD/stability/gSubstitute "stability"for every instanceof "BSOD"found in each input line.s/ *$//Delete all spaces at the end of every line.s/00*/0/gCompress all consecutive sequences of zeroes intoa single zero./GUI/dDelete all lines containing "GUI".s/GUI//gDelete all instances of "GUI", leaving theremainder of each line intact.


Substituting a zero-length string for another is equivalentto deleting that string within a line of input. This leaves theremainder of the line intact. Applying s/GUI//

to the line

 

The most important parts of any application are its GUI and sound effects

results in

 
The most important parts of any application are its  and sound effects

A backslash forces the sedreplacementcommand to continue on to the next line. This has the effect ofusing the newlineat the end of the firstline as the replacement string.

   1 s/^  */\
   2 /g

This substitution replaces line-beginning spaces with anewline. The net result is to replace paragraph indents with ablank line between paragraphs.

An address range followed by one or more operations may requireopen and closed curly brackets, with appropriate newlines.

   1 /[0-9A-Za-z]/,/^$/{
   2 /^$/d
   3 }

This deletes only the first of each set of consecutive blanklines. That might be useful for single-spacing a text file,but retaining the blank line(s) between paragraphs.

A quick way to double-space a text file is sed Gfilename.

For illustrative examples of sed within shell scripts, see:

  1. Example 33-1
  2. Example 33-2
  3. Example 12-3
  4. Example A-2
  5. Example 12-15
  6. Example 12-24
  7. Example A-12
  8. Example A-17
  9. Example 12-29
  10. Example 10-9
  11. Example 12-43
  12. Example A-1
  13. Example 12-13
  14. Example 12-11
  15. Example A-10
  16. Example 17-12
  17. Example 12-16
  18. Example A-28

For a more extensive treatment of sed, check the appropriatereferences in the Bibliography.

Notes

[1]

If no address range is specified, the defaultis alllines.

最后更新:

类似资料

  • 我试图设置专注于“下拉”组件,但焦点不是“下拉”组件。 我使用了中给出的自动对焦属性https://www.primefaces.org/primereact/#/dropdown.然后我尝试使用react ref设置焦点。 https://codesandbox.io/s/dropdownautofocus-jko5d

  • 本文向大家介绍C++ primer基础之容器insert,包括了C++ primer基础之容器insert的使用技巧和注意事项,需要的朋友参考一下 C++ primer基础之容器insert  今天学习C++ 基础知识的时候遇到这样问题,始终出现segments fault。最后才发现原来是自己对“容器insert之后迭代器会失效”的理解不够透彻。 题目如下: 假定iv是一个int的vector,

  • 我在一次将多行插入表时面临问题,因为列id具有主键并基于序列创建。 对于ex: 以下语句创建约束违反错误: 此查询应在表test中插入3行(名称为xxx)。

  • 我目前正在学习Prata的“C Primer Plus”一书。关于第16章的练习问题,我遇到了以下练习: 与数组相比,链表具有更容易添加和删除元素的功能,但排序速度较慢。这就提出了一种可能性:也许将列表复制到数组,对数组进行排序,然后将排序后的结果复制回列表可能比简单地使用列表算法进行排序更快。(但它也可能使用更多的内存。使用以下方法检验速度假设:a。创建一个大型矢量对象 vi0,使用 rand(

  • 自从计算机或机器的发明以来,它们执行各种任务的能力经历了指数级增长。 人类已经开发出计算机系统的功能,包括各种工作领域,它们的速度越来越快,并且随着时间的推移缩小了尺寸。 计算机科学的一个分支,名为人工智能,追求创造像人类一样聪明的计算机或机器。 人工智能的基本概念(AI) 根据人工智能之父约翰麦卡锡的说法,它是“制造智能机器的科学与工程,特别是智能计算机程序”。 人工智能是一种使计算机,计算机控

  • Primer 是 Github 工具包,用于 Github 前端设计。 CSS 框架 以实用程序为中心和BEM风格的组件,为用户提供任何Web项目的构建块。 反应组件 具有封装样式和基于约束的主题道具的演示UI组件。 Octicons 用户的项目、GitHub的图标,在Ruby和JavaScript实现中可用。 Presentations 幻灯片模板可让用户专注于故事,而不必担心设计细节。 接口准

相关阅读