Home > OS >  Shell commands sed for file specifies the final location to the key word in the last line of the con
Shell commands sed for file specifies the final location to the key word in the last line of the con

Time:11-17

Assumes that the file is the test. TXT file content is as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1 Start the 2020-01-01 12:20:20
2 Start Job Jobname
3 SSDFFFF
4 SSDDKDDD
5 Start the 2020-01-01 12:20:50
6 Finish Job Jobname
7 Start the 2020-01-01 12:30:10
8 Start Job Jobname
9 FFHKFFJKFF
10 SSHDKHDKDHKD
Start the 2020-01-01 11 12:30:30
12 Finish Job Jobname
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
We need to print bold part
Use the following shell commands can be achieved:
The cat test. TXT | | tac sed - n '1,/Finish Job/p | sed' $d | tac
Note: output to the screen is the role of the cat file content
Is the role of tac content inversion, the content display in reverse,
[root @ udata TMP] # cat test. TXT | tac
12 Finish Job Jobname
Start the 2020-01-01 11 12:30:30
10 SSHDKHDKDHKD
9 FFHKFFJKFF
8 Start Job Jobname
7 Start the 2020-01-01 12:30:10
6 Finish Job Jobname
5 tart 12:20:50 2020-01-01
4 SSDDKDDD
3 SSDFFFF
2 Start Job Jobname
1 Start the 2020-01-01 12:20:20

Sed - n '1,/Finish the Job/p' role is to line 1 and the first case Finish the Job of line displayed
[root @ udata TMP] # cat test. TXT | | tac sed - n '1,/Finish the Job/p'
12 Finish Job Jobname
Start the 2020-01-01 11 12:30:30
10 SSHDKHDKDHKD
9 FFHKFFJKFF
8 Start Job Jobname
7 Start the 2020-01-01 12:30:10
6 Finish Job Jobname
Sed '$d' role is to remove the last line
[root @ udata TMP] # cat test. TXT | | tac sed - n '1,/Finish Job/p | sed' $d '
12 Finish Job Jobname
Start the 2020-01-01 11 12:30:30
10 SSHDKHDKDHKD
9 FFHKFFJKFF
8 Start Job Jobname
7 Start the 2020-01-01 12:30:10
Then use tac inversion content
[root @ udata TMP] # cat test. TXT | | tac sed - n '1,/Finish Job/p | sed' $d | tac
7 Start the 2020-01-01 12:30:10
8 Start Job Jobname
9 FFHKFFJKFF
10 SSHDKHDKDHKD
Start the 2020-01-01 11 12:30:30
12 Finish Job Jobname
Got what we need

CodePudding user response:

How to identify the bold part of
  • Related