Paul Tanton
Paul Tanton

Reputation: 1

UNIX Replacing a character sequence in either tr or sed

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

Upvotes: 0

Views: 1945

Answers (3)

peak
peak

Reputation: 116870

How can I delete newlines that follow a space?

If you want every occurrence of $' \n' in the original file to be replaced by a space ($' '), and if you know of a character (e.g. a control character) that does not appear in the file, then the task can be accomplished quite simply using sed and tr (as you requested). Let's suppose, for example, that control-A is a character that is not in the file. For the sake of simplicity, let's also assume we can use bash. Then the following script should do the job:

#!/bin/bash
A=$'\01'
tr '\n' "$A" | sed "s/ $A/ /g" | tr "$A" '\n' 

Upvotes: 0

Toby Speight
Toby Speight

Reputation: 30911

It might be simplest with Perl:

perl -p0 -e 's/ \n/ /g'

The -0 flag makes Perl read the entire file as one line. Then we can substitute using s in the usual way. You can, of course, also add the -i option to edit the file in-place.

Upvotes: 1

Toby Speight
Toby Speight

Reputation: 30911

A sed solution is

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

Explanation:

  • / $/: whenever the line ends in space, then
  • N: append a newline and the next line of input, and
  • s/\n//: delete the newline.

Upvotes: 2

Related Questions