How to remove new lines ('\ n', 0x0A) from non-empty lines using tr (1)?

I have a file called file1 with the following contents:

The answer t
o your question 

A conclusive a
nswer isn’t al
ways possible.

When in doubt, ask pe
ople to cite their so
urces, or to explain

Even if we don’t agre
e with you, or tell y
ou.

I would like to convert file1 to file2. The last should look like this:

The answer to your question

A conclusive answer isn’t always possible.

When in doubt, ask people to cite their sources, or to explain

Even if we don’t agree with you, or tell you.

In case I just followed cat file1 | tr -d "\n" > file2", all newlines will be deleted. Ho to remove only those newlines that are on non-empty lines using the utility tr(1)?

+5
source share
6 answers
perl -00 -lpe 'tr/\n//d'

-00 - Perl, . -l , .

+8

, , , , - :

# Assume that the input doesn't contain the '|' character at all
tr '\n' '|' < file1 | sed 's/\([^|]\)|\([^|]\)/\1\2/g' | tr '|' '\n' > file2

|; sed |, ; , , | .

+2

tr , sed

sed -ne '$!H;/^$/{x;s/\n//g;G;p;d;}' file1 > file2

. , . , .

EDIT:

Per @potong comment, , .

sed -ne 'H;/^$/{x;s/\n//g;G;p;};${x;s/\n//g;x;g;p;}' file1 > file2
+2

:

# sed '1{h;d};H;${x;s/\([^\n]\)\n\([^\n]\)/\1\2/g;p};d' file

The answer to your question 

A conclusive answer isn't always possible.

When in doubt, ask people to cite their sources, or to explain

Even if we don't agree with you, or tell you.
+2

file1 :

  • newline,
  • Newline

( -000) , (s/\n\n/\n/g),

$ perl -000 -pe 's/\n\n/\n/g' file1 
The answer t
o your question 
A conclusive a
nswer isn’t al
ways possible.
When in doubt, ask pe
ople to cite their so
urces, or to explain
Even if we don’t agre
e with you, or tell y
ou.

, , file2.

, , ( ), , , .

$ perl -000 -pe 's/(?<=\n)\n//g' file1 
The answer t
o your question 
A conclusive a
nswer isn’t al
ways possible.
When in doubt, ask pe
ople to cite their so
urces, or to explain
Even if we don’t agre
e with you, or tell y
ou.

, , , , , file2.

, file1.

, , , : , , , .

Perl look-around , , , , . " " - (?<!\n). (?!...), (|) ($).

,

$ perl -000 -pe 's/(?<!\n)\n(?!\n|$)//g' file1 
The answer to your question

A conclusive answer isn’t always possible.

When in doubt, ask people to cite their sources, or to explain

Even if we don’t agree with you, or tell you.

, file2, .

perl -000 -pe 's/(?<!\n)\n(?!\n|$)//g' file1  >file2
+2

tr . tr , char -by- char, .

, sed, ( !). (sed master @Sorpigal , !)

awk

/home/shellter:>cat <<-EOS \
| awk 'BEGIN{RS="\n\n"}; { gsub("\n", "", $0) ;printf("%s %s", $0, "\n\n") }'
The answer t
o your question 

A conclusive a
nswer isn’t al
ways possible.

When in doubt, ask pe
ople to cite their so
urces, or to explain

Even if we don’t agre
e with you, or tell y
ou.
EOS


# output
The answer to your question

A conclusive answer isnt always possible.

When in doubt, ask people to cite their sources, or to explain

Even if we dont agree with you, or tell you.

, , dbl-.

Awk , , , , ..

RS = RecordSeperator -- normally a line of data, but a configurable value, that when set 
                     to '\n\n' means a blank line, or a typical separation on a paragraph

$0 = complete line of text (as defined by the internal variables RS (RecordSeparator)
                             In this problem, it is each paragraph of data, viewed though
                             as a record.

$1 = first field in text (as defined by the internal variables FS (FieldSeparator)
                           which defaults to (possibly multiple) space chars OR tab char
                          a line with 2 connected spaces chars and 1 tab char has 3 fields)

NF = Number(of)Fields in current line of data (again fields defined by value of FS as 
                                                described above)

(there are many others, besides, $0, $n, $NF, $FS, $RS).

you can programmatically increase values ​​like $ 1, $ 2, $ 3 using a variable like in the code example, for example $ i (i is a variable that has a number from 2 to NF. says it gives me the value of the i field (i.e. $ 2, $ 3, $ 4 ...)

Hope this helps.

0
source

All Articles