Gandhar Mahadeshwar
Gandhar Mahadeshwar

Reputation: 43

Remove newline if preceded with specific character on non-consecutive lines

I have a text file with every other line ending with a % character. I want to find the pattern "% + newline" and replace it with "%". In other words, I want to delete the newline character right after the % and not the other newline characters.

For example, I want to change the following:

abcabcabcabc%
123456789123
abcabcabcabc%
123456789123

to

abcabcabcabc%123456789123
abcabcabcabc%123456789123

I've tried the following sed command, to no avail.

sed 's/%\n/%/g' < input.txt > output.txt

Upvotes: 4

Views: 1724

Answers (3)

Thor
Thor

Reputation: 47089

In portable sed that supports any number of continued lines:

parse.sed

:a                # A goto label named 'a'
/%$/ {            # When the last line ends in '%'
  N               # Append the next line
  s/\n//          # Remove new-line
  ta              # If new-line was replaced goto label 'a'
}

Run it like this:

sed -f parse.sed infile

Output when infile contains your input and the input from Ed Morton's answer:

abcabcabcabc%123456789123         
abcabcabcabc%123456789123
now is the%winter of%our%discontent

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203149

By default sed can't remove newlines because it reads one newline-separated line at a time.

With any awk in any shell on every UNIX box for any number of lines ending in %, consecutive or not:

$ awk '{printf "%s%s", $0, (/%$/ ? "" : ORS)}' file
abcabcabcabc%123456789123
abcabcabcabc%123456789123

and with consecutive % lines:

$ cat file
now is the%
winter of%
our%
discontent

$ awk '{printf "%s%s", $0, (/%$/ ? "" : ORS)}' file
now is the%winter of%our%discontent

Upvotes: 4

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Your data sample imply that there are no several consecutive lines ending with %.

In that case, you may use

sed '/%$/{N;s/\n//}' file.txt > output.txt

It works as follows:

  • /%$/ - finds all lines ending with %
  • {N;s/\n//} - a block:
    • N - adds a newline to the pattern space, then appends the next line of input to the pattern space
    • s/\n// - removes a newline in the current pattern space.

See the online sed demo.

Upvotes: 4

Related Questions