[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: |
Tue, 26 Jul 2022 22:02:44 -0400 |
In case people start to find this email chain, looking for a way to do what
I did:
sed_expr='
s/[[:blank:]]+(\r?)$/\1/
${
/\r$/!{
x
/\r$/!bdont
x
s/$/\r/
bend
:dont
x
:end
}
}
h
$a\'
sed --binary --regexp-extended --in-place --expression="${sed_expr}" --
"${input_file}"
The last thing worked, but kind of accidentally. I was treating the pair of
'/\r$/{ } /\r$/!{ }' like if-else, but the second one would be considering
the pattern buffer potentially modified within the prior block. What I have
here with branch commands and labels is actually doing if-else.
On Mon, Jul 25, 2022 at 8:58 AM Zachary Santer <zsanter@gmail.com> wrote:
> 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
>