1.The Greater Company :(Ranked 2)
5.United Taxi Service :(Ranked 3)
31.The Cacao Company :(Ranked 4)
40.Monster Inc. :(Ranked 5)
55.Afala, Industrial Strength :(Ranked 6)
60.The North Company :(Ranked 7)
我只想显示公司编号和公司名称。我不想显示该行的其余部分。
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company
我可以通过 sed 's/: .* //' filename.txt 来完成此操作。我将如何使用带有反向引用的模式来实现上述结果。我想要匹配的模式是 ([0-9]* .* :)。
答案1
如果所有行都以 开头,则[0-9]*
无需显式匹配它:只需捕获(但不包括):
分隔符
sed 's/\(^[^:]*\) :.*/\1/' file
1.The Greater Company
5.United Taxi Service
31.The Cacao Company
40.Monster Inc.
55.Afala, Industrial Strength
60.The North Company