从一个大的输入文件中只提取与几个模式中的每一个匹配的最后一行



我有一个文本文件,如下所示。在执行命令的过程中,输出会像这样打印出来。

100 files
200 files
300 files
400 files
500 files
600 files
700 files
800 files
900 files
73700 files
73800 files
73900 files
74000 files
74100 files
74200 files
74300 files
74400 files
74498 text files.
classified 74484 files
Duplicate file check 74484 files (19369 known unique)                                        
Unique:    17800 files                                          
Unique:    17900 files                                          
Unique:    18000 files                                          
Unique:    18100 files                                          
Unique:    18200 files                                          
Unique:    18300 files                                          
Unique:    18400 files                                          
Unique:    18500 files                                          
Unique:    18600 files                                          
Unique:    18700 files                                          
Unique:    18800 files                                          
Unique:    18900 files                                          
Unique:    19000 files                                          
Unique:    19100 files                                          
Unique:    19200 files                                          
Unique:    19300 files                                          
49208 unique files.                              
Counting:  47000
Counting:  47100
Counting:  47200
Counting:  47300
Counting:  47400
Counting:  47500
Counting:  47600
Counting:  47700
Counting:  47800
Counting:  47900
Counting:  48000
Counting:  48100
Counting:  48200
Counting:  48300
Counting:  48400
Counting:  48500
Counting:  48600
Counting:  48700
Counting:  48800
Counting:  48900
Counting:  49000
Counting:  49100
Counting:  49200
28105 files ignored.

我只需要最后几行。

74498 text files.
Unique:    19300 files                                          
Counting:  49200

我想使用sed命令,但它删除了所有的行。但我需要守住最后一条线。你能给我建议一些模式或命令来为这个需求编写代码吗?

我尝试了这种模式,删除了以";文件";但它删除了其他行,比如以";分类并以";文件";。但我只想删除以数字开头的行。

sed '/[0-9] files$/d' gg | sed '/^Unique:/d' | sed '/^Counting:/d' | tee gg

我想要的输出:

74400 files
74498 text files.
classified 74484 files
Duplicate file check 74484 files (19369 known unique)                                                                                 
Unique:    19300 files                                          
49208 unique files.                              
Counting:  49200
28105 files ignored.
awk '/text files/    {$1=$1; a=$0}
$1=="Unique:"   {b=$0}
$1=="Counting:" {c=$0}
END{print a ORS b ORS c}' file

或者作为一行:

awk '/text files/{$1=$1; a=$0} $1=="Unique:"{b=$0} $1=="Counting:"{c=$0} END{print a ORS b ORS c}' file

输出:

74498个文本文件。唯一:19300个文件计数:49200

$1=$1强制awk使用其默认的输入和输出分隔符重建当前行。在这种情况下,这将删除前导空格。

请参阅:8个强大的Awk内置变量–FS、OFS、RS、ORS、NR、NF、FILENAME、FNR-

假设有结果的文件是result_file:

grep "text files" result_file; grep "Unique:" result_file | tail -1; grep "Counting:" result_file | tail -1;

相关内容

最新更新