user3200676
user3200676

Reputation:

bash script to remove newline

I am trying to remove newlines from a file. My file is like this (it contains backward slashes):

line1\|
line2\|

I am using the following script to remove newlines:

#!/bin/bash
INPUT="file1"
while read line
do
: echo -n $line
done < $INPUT

I get the following output:

line1|line2|

It removes the backslashes. How can I retain those backslashes?

Upvotes: 0

Views: 1517

Answers (2)

Barmar
Barmar

Reputation: 782499

The -r option to read prevents backslash processing of the input.

while read -r line
do
    echo -n "$line"
done < $INPUT

But if you just want to remove all newlines from the input, the tr command would be better:

tr -d '\n' < $INPUT

Upvotes: 6

DopeGhoti
DopeGhoti

Reputation: 767

Try sed 's/\n//' /path/to/file

Upvotes: -1

Related Questions