Using the sed p command, you can print the content of the current mode space.
You may wonder why the p command is required. This is because after the sed command is executed, sed prints the content in the mode space by default.
You will see several reasons: the p command allows you to control which content will be printed to the standard output stdout ). Generally, when the p command is used, the-n option is used to prevent the default output of sed. Otherwise, if you only use the p print command, the matching line will be displayed twice.
In the following example, each row of exployee.txt is displayed twice:
$ Sed 'P' employee.txt
101, John Doe, CEO
101, John Doe, CEO
102, Jason Smith, IT Manager
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
104, Anand Ram, Developer
105, Jane Miller, Sales Manager
105, Jane Miller, Sales Manager
Print each line at a time similar to 'cat exployee.txt '):
$ Sed-n 'P' employee.txt
101, John Doe, CEO
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
105, Jane Miller, Sales Manager
Specify a range
If the match range is not specified in the sed command, it matches all rows by default. The following examples demonstrate that the range is specified when sed is executed.
Print only 2nd rows:
$ Sed-n'2 P' employee.txt
102, Jason Smith, IT Manager
Print rows from 1st to 4th:
$ Sed-n '1, 4 P' employee.txt
101, John Doe, CEO
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
Print from 2 rows to the last line. $ indicates the last line ):
$ Sed-n' 2, $ P' employee.txt
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
105, Jane Miller, Sales Manager
Modify address range
You can also use ",", "+" and ~ Modify the address range representation.
In the above example, we have seen several examples that use commas (,) to indicate the range. It means obviously: n, m displays rows from n to m.
Plus sign +) can be used with commas (,) to specify a number, rather than the specific row. For example, n, + m indicates the m row from the nth row and the following.
Tilde ~) It can also be used in the address range. It means a few hops each time. For example, the address range is n ~ M indicates that sed should start at line n and span m rows each time.
1 ~ 2 matches 1, 3, 5, 7, and so on.
2 ~ 2. Match 2, 4, 6, 8, and so on.
1 ~ 3 matches 1, 4, 7, 10, and so on.
2 ~ 3 matches, and so on.
Print only odd rows:
$ Sed-n' 1 ~ 2 p'employee.txt
101, John Doe, CEO
103, Raj Reddy, Sysadmin
105, Jane Miller, Sales Manager
Pattern Matching
As you can specify a number to indicate the address or range), or specify a matching mode or mode range), the following are several examples.
Print the rows containing "Jane:
$ Sed-n'/Jane/P' employee.txt
105, Jane Miller, Sales Manager
Print the row that matches Jason for the first time to 4th rows:
$ Sed-n'/Jason/, 4 P' employee.txt
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
NOTE: If no line matching "Jason" is found before row 4th, this command prints the line containing "Jason" after row 4.
Print the line from the first matched "AJ" to the last line:
$ Sed-n'/Raj/, $ P' employee.txt
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
105, Jane Miller, Sales Manager
Print the line that matches "AJ" to the line that matches "Jane:
$ Sed-n'/Raj/,/Jane/P' employee.txt
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer
105, Jane Miller, Sales Manager
Print the line matching "Jason" and the two lines following it:
$ Sed-n'/Jason/, + 2 P' employee.txt
102, Jason Smith, IT Manager
103, Raj Reddy, Sysadmin
104, Anand Ram, Developer