剥离后如何打印以特定字符串结尾的所有行?

How to print all lines ending with a particular string after stripping it?

我想打印所有末尾有这个字符串的行 - /Season $N .这里 $N 是可以从 0 或 01 开始的任何数字。最后在打印这些行之前,我想从其末尾剥离 /Season $N 并删除重复项(如果有的话)(可以只使用 sort -u )

猫file.txt

SomeDir/path3/File.ext
SomeDir/Path/Season 5
SomeDir/path/Season 6
SomeDir/path/path/Season 10
SomeDir/path2/Season 19
Some Random String
SomeDir/path/path/Season 19
SomeDir/test/Season 56

预期输出

SomeDir/Path
SomeDir/path/path
SomeDir/path2
SomeDir/test

这会做你想做的事

cat file.txt | awk -F/ -vOFS=/ '/Season/{NF--;print}' | sort -uf

(编辑)但这样更好:

awk -F/ -vOFS=/ '$NF ~ /Season [0-9]+/{NF--;print}' < file.txt | sort -fu