[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Add missing final line ending to files - LF or CRLF
From: |
Zachary Santer |
Subject: |
Re: Add missing final line ending to files - LF or CRLF |
Date: |
Mon, 25 Jul 2022 08:58:53 -0400 |
On Mon, Jul 25, 2022 at 3:58 AM Zachary Santer <zsanter@gmail.com> wrote:
>
> 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}"
If the final newline I'm getting in this case is coming from the
contents of hold space which I'm appending on, I wasn't able to get
rid of it by changing 's/\n//' into 's/\n//g', but I guess that kind
of makes sense.
I finally settled on this, which is a lot easier to reason about:
sed_expr='
s/[[:blank:]]+(\r?)$/\1/
${
/\r$/!{
x
/\r$/{
x
s/$/\r/
}
/\r$/!{
x
}
}
}
h
$a\'
sed --binary --regexp-extended --in-place --expression="${sed_expr}" -- "${1}"
To use '$a/' like this, it has to be the very last argument in the
expression. Luckily for me, the h command could come before it. The
idea came from
here<https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file>.
Zack