zsh-users
 help / color / mirror / code / Atom feed
* Append newline to many files
@ 2022-09-25 13:19 Dominik Vogt
  2022-09-25 13:30 ` Roman Perepelitsa
  0 siblings, 1 reply; 5+ messages in thread
From: Dominik Vogt @ 2022-09-25 13:19 UTC (permalink / raw)
  To: Zsh Users

Assume there are thousands of text files that are not terminated
with a newline.  I want to concatenate them all, but add the
missing newline between files.

This works but takes ten times as much time as "cat foo.*".

  for i in foo.*; do cat "$i"; echo; done > out

I can't really think of a fast yet simple solution.

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Append newline to many files
  2022-09-25 13:19 Append newline to many files Dominik Vogt
@ 2022-09-25 13:30 ` Roman Perepelitsa
  2022-09-26  8:11   ` zzapper
  0 siblings, 1 reply; 5+ messages in thread
From: Roman Perepelitsa @ 2022-09-25 13:30 UTC (permalink / raw)
  To: dominik.vogt, Zsh Users

On Sun, Sep 25, 2022 at 3:21 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
>
> Assume there are thousands of text files that are not terminated
> with a newline.  I want to concatenate them all, but add the
> missing newline between files.
>
> This works but takes ten times as much time as "cat foo.*".
>
>   for i in foo.*; do cat "$i"; echo; done > out
>
> I can't really think of a fast yet simple solution.

This should work:

    print >lf
    files=(foo.*)
    lf=(lf)
    cat -- ${files:^^lf}
    rm lf

Roman.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Append newline to many files
  2022-09-25 13:30 ` Roman Perepelitsa
@ 2022-09-26  8:11   ` zzapper
  2022-09-26  8:51     ` Dominik Vogt
  0 siblings, 1 reply; 5+ messages in thread
From: zzapper @ 2022-09-26  8:11 UTC (permalink / raw)
  To: zsh-users


On 25/09/2022 14:30, Roman Perepelitsa wrote:
> On Sun, Sep 25, 2022 at 3:21 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
>> Assume there are thousands of text files that are not terminated
>> with a newline.  I want to concatenate them all, but add the
>> missing newline between files.
>>
>> This works but takes ten times as much time as "cat foo.*".
>>
>>    for i in foo.*; do cat "$i"; echo; done > out
>>
>> I can't really think of a fast yet simple solution.
> This should work:
>
>      print >lf
>      files=(foo.*)
>      lf=(lf)
>      cat -- ${files:^^lf}
>      rm lf
>
> Roman.
>
This looks interesting but any chance of an explanation of how it works 
for us lesser mortals please?

zzapper



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Append newline to many files
  2022-09-26  8:11   ` zzapper
@ 2022-09-26  8:51     ` Dominik Vogt
  2022-09-26 19:39       ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Dominik Vogt @ 2022-09-26  8:51 UTC (permalink / raw)
  To: zsh-users

On Mon, Sep 26, 2022 at 09:11:04AM +0100, zzapper wrote:
>
> On 25/09/2022 14:30, Roman Perepelitsa wrote:
> > On Sun, Sep 25, 2022 at 3:21 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
> > > Assume there are thousands of text files that are not terminated
> > > with a newline.  I want to concatenate them all, but add the
> > > missing newline between files.
> > >
> > > This works but takes ten times as much time as "cat foo.*".
> > >
> > >    for i in foo.*; do cat "$i"; echo; done > out
> > >
> > > I can't really think of a fast yet simple solution.
> > This should work:
> >
> >      print >lf
> >      files=(foo.*)
> >      lf=(lf)
> >      cat -- ${files:^^lf}
> >      rm lf
> >
> This looks interesting but any chance of an explanation of how it works for
> us lesser mortals please?

It first generates a file "lf" that contains only a newline and
stores the filename in an array "lf", and the names of the files
in question in an array "files".

"${files:^^lf}" merges the two arrays in an interleaving fashion.
The result is file.1 lf file.2 lf file.3 lf ... (The shorter array
is repeated up to the other one's length).

Quite clever solution, but a bit complicated.

--

This one works too and is simple and fast:

  $ sed -e '$s/$/\n/' -e <somereplacement> foo.*
           ^^^
            |
       Replacement is done only on the last line

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Append newline to many files
  2022-09-26  8:51     ` Dominik Vogt
@ 2022-09-26 19:39       ` Mikael Magnusson
  0 siblings, 0 replies; 5+ messages in thread
From: Mikael Magnusson @ 2022-09-26 19:39 UTC (permalink / raw)
  To: zsh-users

On 9/26/22, Dominik Vogt <dominik.vogt@gmx.de> wrote:
> On Mon, Sep 26, 2022 at 09:11:04AM +0100, zzapper wrote:
>>
>> On 25/09/2022 14:30, Roman Perepelitsa wrote:
>> > On Sun, Sep 25, 2022 at 3:21 PM Dominik Vogt <dominik.vogt@gmx.de>
>> > wrote:
>> > > Assume there are thousands of text files that are not terminated
>> > > with a newline.  I want to concatenate them all, but add the
>> > > missing newline between files.
>> > >
>> > > This works but takes ten times as much time as "cat foo.*".
>> > >
>> > >    for i in foo.*; do cat "$i"; echo; done > out
>> > >
>> > > I can't really think of a fast yet simple solution.
>> > This should work:
>> >
>> >      print >lf
>> >      files=(foo.*)
>> >      lf=(lf)
>> >      cat -- ${files:^^lf}
>> >      rm lf
>> >
>> This looks interesting but any chance of an explanation of how it works
>> for
>> us lesser mortals please?
>
> It first generates a file "lf" that contains only a newline and
> stores the filename in an array "lf", and the names of the files
> in question in an array "files".
>
> "${files:^^lf}" merges the two arrays in an interleaving fashion.
> The result is file.1 lf file.2 lf file.3 lf ... (The shorter array
> is repeated up to the other one's length).
>
> Quite clever solution, but a bit complicated.

And here's a oneliner version,
() { cat -- ${@[2,-1]:^^1} } =(echo) foo.*

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-26 19:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-25 13:19 Append newline to many files Dominik Vogt
2022-09-25 13:30 ` Roman Perepelitsa
2022-09-26  8:11   ` zzapper
2022-09-26  8:51     ` Dominik Vogt
2022-09-26 19:39       ` Mikael Magnusson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).