Professional Web Applications Themes

multiple cat-ting - UNIX Programming

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...

  1. #1

    Default multiple cat-ting

    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
    Bryan Harris Guest

  2. #2

    Default Re: multiple cat-ting

    On Wed, 02 Jul 2003 at 16:41 GMT, Bryan Harris wrote:
    > 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 [url]http://cfaj.freeshell.org[/url]
    ================================================== =================
    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
    Chris F.A. Johnson Guest

  3. #3

    Default Re: multiple cat-ting

    Bryan Harris wrote:
    > 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

    --
    Stéphane
    Stephane CHAZELAS Guest

  4. #4

    Default Re: multiple cat-ting

    [email]harrisbdakotacom.net[/email] (Bryan Harris) writes:
    > 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
    Joe Halpin Guest

  5. #5

    Default Re: multiple cat-ting

    On Wed, 02 Jul 2003 at 17:07 GMT, Stephane CHAZELAS wrote:
    > Bryan Harris wrote:
    >> 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.
    > 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 [url]http://cfaj.freeshell.org[/url]
    ================================================== =================
    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
    Chris F.A. Johnson Guest

  6. #6

    Default Re: multiple cat-ting

    > 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
    Bryan Harris Guest

  7. #7

    Default Re: multiple cat-ting

    In article <49baad81.0307021301.355adaf9posting.google.com >,
    Bryan Harris <harrisbdakotacom.net> wrote:
    >> 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.

    --
    Barry Margolin, [email]barry.margolinlevel3.com[/email]
    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.
    Barry Margolin Guest

  8. #8

    Default Re: multiple cat-ting

    Chris F.A. Johnson wrote:
    [...]
    >> 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.

    --
    Stéphane
    Stephane CHAZELAS Guest

  9. #9

    Default Re: multiple cat-ting

    > Bryan Harris <harrisbdakotacom.net> wrote:
    > >> 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.
    > 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
    Bryan Harris Guest

  10. #10

    Default Re: multiple cat-ting

    [email]harrisbdakotacom.net[/email] (Bryan Harris) writes:
    > 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
    Micah Cowan Guest

Similar Threads

  1. Q: LWP::Simple not get()-ting.
    By Jeff Bernstein in forum PERL Modules
    Replies: 2
    Last Post: June 11th, 08:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139