综合技术

UNIX Replacing a sequence of characters in tr or sed

微信扫一扫,分享到朋友圈

UNIX Replacing a sequence of characters in tr or sed
0

Have a file that has been created incorrectly. There are several space delimited fields in the file but one text field has some unwanted newlines. This is causing a big problem.

How can I remove these characters but not the wanted line ends?

file is:

'Number field' 'Text field'                    'Number field'
   1            Some text                       999999
   2            more
text                             111111111
   3            Even more text                  8888888888
EOF

So there is a NL after the word “more”.

I’ve tried sed:

sed 's/.$//g' test.txt > test.out

and

sed 's/n//g' test.txt > test.out

But none of these work. The newlines do not get removed.

tr -d 'n'
does too much – I need to remove ONLY the newlines that are preceded by a space.

How can I delete newlines that follow a space?

SunOS 5.10 Generic_144488-09 sun4u sparc SUNW,Sun-Fire-V440

A sed solution is

sed '/ $/{N;s/n//}'

Explanation:

/ $/
N
s/n//
阅读原文...

微信扫一扫,分享到朋友圈

UNIX Replacing a sequence of characters in tr or sed
0
Hello, buddy!

Apple achieves 'Mission: Impossible' of reaching 2 billion iOS device shipments

上一篇

Chinese Media accuses 90% Cryptocurrencies of having Stolen Code

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

UNIX Replacing a sequence of characters in tr or sed

长按储存图像,分享给朋友