Remark: I'm working with .tex (TeX) file.

In TeX code, line break does not affect the output. To produce a new paragraph we have to use the \par command.

Some TeX editors interpret a blank line as \par. So the following code

line one here
continues here and so on

New paragraph here

produces the output

line one here continues here and so on

New paragraph here

Since some editors can wrap long lines automatically I like to write my codes with less RETURN as possible. This is better when we want to take care of line numbers.

See the screen-shot:

enter image description here

I'd like to remove the end of lines but keep the blank lines even if they contain only white spaces. After the process I'd like my code like this:

enter image description here

Note that the new code contains only 3 lines.

I tried using tr but no success.

Edit: after testing the code suggested by @lcd047, I noticed that some problems appear when the line n contains % (% is used to comment in TeX and everything after it in the line is ignored).

So, I guess that I should add the request: if line n contains % don't glue n with n-1 neither don't glue n+1 with n.

2 Answers 2


Maybe something like:

awk '!NF || /%/ {printf "%s", (NR > 1 ? "\n" : "") $0; sep = "\n"; next}
     {printf "%s", sep $0; sep = " "}
     END {if (NR) print ""}' < file.in > file.out
  • Thanks. Please, I edited the request. Is it possible to adapt your code?
    – Sigur
    May 1, 2015 at 11:30
  • @Sigur, see edit. May 1, 2015 at 11:54
  • Nice! It works. Does make sense to use || /%/ || /\/ to protect also lines starting with back slash? Many lines on preamble of tex files starts with `\` so it is natural to keep one above others.
    – Sigur
    May 1, 2015 at 12:07

Try this:

perl -lpe 'BEGIN{ $/= $\ = "\n\n" } s/^[^%]+?\K\n/ /gmo' <file_old.tex >file_new.tex

To also leave alone lines staring with commands:

perl -lpe 'BEGIN{ $/= $\ = "\n\n" } s/^\s*[^\s\\%][^%]*?\K\n/ /gmo' <file_old.tex >file_new.tex

Please note however that some of the people reviewing your papers will hate you with a passion for this move.

  • Well, usually they don't know that the editor can wrap lines automatically. I'm trying your code. How to save the result in a new file?
    – Sigur
    May 1, 2015 at 10:36
  • Some editors (e.g. Vim) can wrap lines automatically but make it hard for the user to navigate through continuation lines. To save the result: awk ... file.tex > newfile.tex.
    – lcd047
    May 1, 2015 at 10:40
  • Ow, thanks. It works. But some problems appear when there are commented lines with % in the middle. After gluing the lines, anything after % is commented. I don't see a way to solve this.
    – Sigur
    May 1, 2015 at 10:43
  • Maybe, doing this: if the line n contains % anywhere, don't move the line n+1 to the end of line n.
    – Sigur
    May 1, 2015 at 10:46
  • Right. I edited my answer to address comments.
    – lcd047
    May 1, 2015 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .