嗨,我有一个数据格式如下的文件:

262353824192
Motley Crue Too Fast For Love Vinyl LP Leathur Records LR123 rare 3rd pressing
http://www.ebay.co.uk/itm/Motley-Crue-Too-Fast-Love-Vinyl-LP-Leathur-Records-LR123-rare-3rd-pressing-/262353824192



301870324112
TRAFFIC Same UK 1st press vinyl LP in gatefold / booklet sleeve Island pink eye
http://www.ebay.co.uk/itm/TRAFFIC-Same-UK-1st-press-vinyl-LP-gatefold-booklet-sleeve-Island-pink-eye-/301870324112



141948187203
NOW That's What I Call Music LP'S Joblot 2-14 MINT CONDITION Vinyl
http://www.ebay.co.uk/itm/NOW-Thats-Call-Music-LPS-Joblot-2-14-MINT-CONDITION-Vinyl-/141948187203

我想用一根管子代替一根新的管子,但保留原来的两条新管子。我试过:
tr '\n' '|' < text.txt

但这将用|取代所有新的生产线,因此不同的产品不再在不同的生产线上。我基本上希望在产品编号、标题和url之间有一个|分隔符,但是每一个单独的产品在不同的行上。我怎样才能做到这一点?

最佳答案

使用tr和一点sed:

tr "\n" "|" < text.txt | sed 's/||\+/\n/g'

关于linux - 使用tr替换单个新行,但不能替换多个新行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36579357/

10-14 16:26