sed-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Add missing final line ending to files - LF or CRLF


From: Zachary Santer
Subject: Add missing final line ending to files - LF or CRLF
Date: Mon, 25 Jul 2022 03:58:36 -0400

Along with removing trailing tabs and spaces from all lines.

sed_expr='
s/[[:blank:]]+(\r?)$/\1/
${
  /\r$/!{
    x
    s/^[^\r]*(\r?)$/\1/
    x
    G
    s/\n//
#   a\
#
  }
}
h
'

sed --binary --regexp-extended --in-place --expression="${sed_expr}" -- "${1}"

I'm trying to allow for MS-DOS-style line endings on *nix machines and
vice versa. Thus, using '--binary' and the '\r's.

The first line is simply removing tabs and spaces at the end of a
line, potentially followed by a CR, and then putting the CR back in
place. The final line is putting the pattern space into the hold space
for use in the next cycle.

If the final line of the input file ends in a CR, we assume the LF
follows it immediately and take no further action.

Otherwise, we exchange pattern and hold spaces, to get the previous
line we output in the pattern space again. From there, we remove
everything but a possible final CR. We swap pattern and hold spaces
again. And then we append the hold space to the pattern space, with an
LF between them. We don't want that LF in there, so we remove it in
the next line.

Thus, if the second to last line ended in CRLF, we append a CR to the
final line. If the second to last line ended in LF, we append nothing
to the final line.

I thought I needed the two commented-out lines to then follow my final
pattern space with an LF. However, with those lines not commented out,
I end up with a final line that ends with CRLFLF or LFLF. And with the
lines commented out, I get what I actually want, CRLF or LF.

I don't understand why. Could someone please explain to me where the
final LF I'm getting is coming from?

Thanks,
Zack



reply via email to

[Prev in Thread] Current Thread [Next in Thread]