选择所有其他新行

时间:2017-11-07 16:43:36

标签: regex

说我有以下文本文件:

Spicy jalapeno bacon ipsum dolor amet
chuck pancetta capicola rump biltong 
tri-tip. Pancetta t-bone picanha, pork
loin porchetta beef tri-tip ball tip 
turkey tenderloin short loin tail
pastrami bacon. Boudin tri-tip sirloin, 
capicola shank short loin pork loin
brisket swine. Shankle pork loin 
andouille, spare ribs alcatra turducken
tail pork belly burgdoggen beef ribs 
capicola swine strip steak rump. Tongue
doner drumstick meatball. Biltong 
landjaeger beef ribs ball tip
prosciutto, doner kevin picanha beef kielbasa 
hamburger bresaola.

我想选择所有其他新行。我怎样才能做到这一点?如果可能,为了将来参考,我如何选择每个 x 新行?

我已经尝试\n(?=.*)但不知道该怎么做。

编辑我没有尝试选择整行,而是选择行缩进。 \nSpicy jalapeno bacon ipsum dolor amet chuck pancetta capicola rump biltong tri-tip. Pancetta t-bone picanha, pork loin porchetta beef tri-tip ball tip 。我希望它可以选择,所以我可以用空格替换它。例如,前四行将是

{{1}}

1 个答案:

答案 0 :(得分:0)

很好用awk它还是一个简单的命令:

# Keeps 1 new line every `4` ones
awk '{ lin = sprintf("%s %s",lin,$0); if ((NR)%4==0) { print lin; lin= ""; } } END { print lin;}' test.txt