multiple cat-ting

multiple cat-ting

Post by Bryan Harr » Fri, 04 Jul 2003 01:41:34



Is there an easy way to append a small file, tmp1, to a bunch of files
all at once?  This doesn't work:

/bin/ls -1 *.vsr | xargs cat tmp1 >>

I think the shell is intercepting the >>, and enclosing the last 3
items in single quotes doesn't work either.

- B

 
 
 

multiple cat-ting

Post by Chris F.A. Johnso » Fri, 04 Jul 2003 02:06:33



> Is there an easy way to append a small file, tmp1, to a bunch of files
> all at once?  This doesn't work:

> /bin/ls -1 *.vsr | xargs cat tmp1 >>

> I think the shell is intercepting the >>, and enclosing the last 3
> items in single quotes doesn't work either.

tmp1=`cat tmp1`
for file in *.vsr
do
   echo "$tmp1" >> "$file"
done

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

multiple cat-ting

Post by Stephane CHAZELA » Fri, 04 Jul 2003 02:07:05



> Is there an easy way to append a small file, tmp1, to a bunch of files
> all at once?  This doesn't work:

> /bin/ls -1 *.vsr | xargs cat tmp1 >>

> I think the shell is intercepting the >>, and enclosing the last 3
> items in single quotes doesn't work either.

">>" is a shell operator, not a xargs operator.

Here, you want

tee -a ./*.vsr < tmp1 > /dev/null

or

for f in *.vsr; do cat tmp1 >> "$f"; done

--
Stphane

 
 
 

multiple cat-ting

Post by Chris F.A. Johnso » Fri, 04 Jul 2003 02:23:02




>> Is there an easy way to append a small file, tmp1, to a bunch of files
>> all at once?  This doesn't work:

>> /bin/ls -1 *.vsr | xargs cat tmp1 >>

>> I think the shell is intercepting the >>, and enclosing the last 3
>> items in single quotes doesn't work either.

> ">>" is a shell operator, not a xargs operator.

> Here, you want

> tee -a ./*.vsr < tmp1 > /dev/null

    Good one! I keep forgetting that tee can take multiple filenames.

Quote:> or

> for f in *.vsr; do cat tmp1 >> "$f"; done

    There's no need to call cat for every file; just store the file in
    a variable first with "tmp1=`cat tmp1`", then echo it to each file.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

multiple cat-ting

Post by Joe Halpi » Fri, 04 Jul 2003 02:17:12



> Is there an easy way to append a small file, tmp1, to a bunch of files
> all at once?  This doesn't work:

> /bin/ls -1 *.vsr | xargs cat tmp1 >>

> I think the shell is intercepting the >>, and enclosing the last 3
> items in single quotes doesn't work either.

If cat has more than two arguments it will append all but the last
argument to the last argument, which I don't think is what you want.

One possibility:

ls *.vsr | while read f;do cat tmp1 >> $f;done

Or, to keep from invoking cat repeatedly:

x=`cat tmp1`; ls *.vsr | while read f;do echo "$x" >> $f;done

Joe

 
 
 

multiple cat-ting

Post by Bryan Harr » Fri, 04 Jul 2003 06:01:08


Quote:> tee -a ./*.vsr < tmp1 > /dev/null

This is cool!  How does it work?  I tried leaving off the trailing >
/dev/null and it filled up the three files with the contents of the
first...

- B

 
 
 

multiple cat-ting

Post by Barry Margoli » Fri, 04 Jul 2003 06:34:50




>> tee -a ./*.vsr < tmp1 > /dev/null

>This is cool!  How does it work?  I tried leaving off the trailing >
>/dev/null and it filled up the three files with the contents of the
>first...

The tee command copies its standard input (which is redirected from tmp1)
to standard output *and* to all its filename arguments.  The -a option
causes tee to append to the files rather than replacing their contents.
And > /dev/null at the end causes the standard output to be discarded.

The only difference if you leave off the trailing > /dev/null is that you
should have seen the contents of tmp1 on your screen as well as it filling
up the *.vsr files.

--

Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

multiple cat-ting

Post by Stephane CHAZELA » Fri, 04 Jul 2003 09:06:38


[...]

Quote:>> for f in *.vsr; do cat tmp1 >> "$f"; done

>     There's no need to call cat for every file; just store the file in
>     a variable first with "tmp1=`cat tmp1`", then echo it to each file.

Except that `...` strips the trailing newlines and echo adds one
and may process '\' sequences and/or would fail for a "-n" input
and/or may be an utility as big as cat (depending on your
shell) and this won't work if tmp1 contain '\0' chars.

If tmp1 is a big file, cat will probably be faster and take less
resources anyway. If tmp1 is small, it will be in memory cache
anyway as well as cat code, so that wouldn't make much
difference in terms of disk access.

--
Stphane

 
 
 

multiple cat-ting

Post by Bryan Harr » Fri, 04 Jul 2003 11:36:47



> >> tee -a ./*.vsr < tmp1 > /dev/null

> >This is cool!  How does it work?  I tried leaving off the trailing >
> >/dev/null and it filled up the three files with the contents of the
> >first...

My bad, one of the files caught by the wildcard is the one I used for
the redirect.  Oops.

Quote:> The tee command copies its standard input (which is redirected from tmp1)
> to standard output *and* to all its filename arguments.  The -a option
> causes tee to append to the files rather than replacing their contents.
> And > /dev/null at the end causes the standard output to be discarded.
> The only difference if you leave off the trailing > /dev/null is that you
> should have seen the contents of tmp1 on your screen as well as it filling
> up the *.vsr files.

Very slick, this is exactly what I wanted.  Thanks!

- Bryan

 
 
 

multiple cat-ting

Post by Micah Cowa » Sun, 06 Jul 2003 15:14:34



> Is there an easy way to append a small file, tmp1, to a bunch of files
> all at once?  This doesn't work:

> /bin/ls -1 *.vsr | xargs cat tmp1 >>

> I think the shell is intercepting the >>, and enclosing the last 3
> items in single quotes doesn't work either.

That's because then the shell *won't* get the >>, and cat sure won't
know what to do with it...

Try:

  for vsr in `ls *.vsr`; do cat tmp1 >> "$vsr"; done

But I don't think that'll work if any filenames contain spaces.

-Micah

 
 
 

1. Follow-up (WAS cat'ting a binary file blows console)

  Vikky,

  Try echo ^V^O at the garbled prompt.

  Second piece of advice; please people,
  I hate it when the followup's are
  redirected to ones mail box instead of
  the newsgroup itself. Others could
  appreciate the answers too and my newsreader
  wouldn't complain...

  Stef

--
Steven Buytaert



        'Imagination is more important than knowledge.'
                        (A. Einstein)

2. network failed after pppoe re-connect

3. cat'ting to /dev/dsp stops after 2 seconds

4. startx options

5. Q:deleting mail headers in multiple files "catted" togather

6. Linux & FreeBSD

7. {multiple} Kernel Oops on cat /proc/ioports

8. Video Card Problem

9. cat multiple files onto list

10. cat , lp.cat and streams

11. cat vs de-cat

12. Solaris 8 box: cat file | rsh solaris_2.6_box "cat > /dev/null" SLOW

13. CAT 5 vs CAT 6