perl one-liner question: add file into text.

Discussion in 'Perl Misc' started by ahq_2001, May 13, 2005.

  1. ahq_2001

    ahq_2001 Guest

    Hi, Guys:

    I think perl should have the same ability to do this, but just not sure
    how. Any help is welcomed!!!

    Directory A (/home/dir_a) have these files:
    file_A, file_B, file_C.

    Directory B (/home/dir_b) have the corresponding notes:
    file_A_note, file_B_note, file_C_note.


    To append file_X_note into file_X at the end of the line
    "Put_Note_Here" in file_A:
    I know foreach + sed can do this with:

    In dir_A: use

    foreach f (*)
    > sed "/Put_Note_Here/ r ../dir_b/$f.note" $f >! tmp1 && mv tmp1 $f
    > end


    Can any one help to show how can I use perl one-liner ( and/or shell
    "foreach" ) to achieve the same function? (No offend to sed lover, but
    I just prefer using perl... )

    Many thanks.

    AhQ
    ahq_2001, May 13, 2005
    #1
    1. Advertising

  2. ahq_2001

    Anno Siegel Guest

    ahq_2001 <> wrote in comp.lang.perl.misc:
    > Hi, Guys:
    >
    > I think perl should have the same ability to do this, but just not sure
    > how. Any help is welcomed!!!
    >
    > Directory A (/home/dir_a) have these files:
    > file_A, file_B, file_C.
    >
    > Directory B (/home/dir_b) have the corresponding notes:
    > file_A_note, file_B_note, file_C_note.
    >
    >
    > To append file_X_note into file_X at the end of the line
    > "Put_Note_Here" in file_A:
    > I know foreach + sed can do this with:
    >
    > In dir_A: use
    >
    > foreach f (*)
    > > sed "/Put_Note_Here/ r ../dir_b/$f.note" $f >! tmp1 && mv tmp1 $f
    > > end

    >
    > Can any one help to show how can I use perl one-liner ( and/or shell
    > "foreach" ) to achieve the same function? (No offend to sed lover, but
    > I just prefer using perl... )


    So what did you try?

    We're not in the business of translating scripts from whatever
    language the poster happens to know. Take a look at s2p for sed.

    Anno
    Anno Siegel, May 14, 2005
    #2
    1. Advertising

  3. ahq_2001

    Tsu Do Nimh Guest

    ahq_2001 wrote:
    > Hi, Guys:
    >
    > I think perl should have the same ability to do this, but just not sure
    > how. Any help is welcomed!!!
    >
    > Directory A (/home/dir_a) have these files:
    > file_A, file_B, file_C.
    >
    > Directory B (/home/dir_b) have the corresponding notes:
    > file_A_note, file_B_note, file_C_note.
    >
    >
    > To append file_X_note into file_X at the end of the line
    > "Put_Note_Here" in file_A:
    > I know foreach + sed can do this with:
    >
    > In dir_A: use
    >
    > foreach f (*)
    >> sed "/Put_Note_Here/ r ../dir_b/$f.note" $f >! tmp1 && mv tmp1 $f
    >> end

    >
    > Can any one help to show how can I use perl one-liner ( and/or shell
    > "foreach" ) to achieve the same function? (No offend to sed lover, but
    > I just prefer using perl... )
    >
    > Many thanks.
    >
    > Ah


    I know this one. Yay!

    =8<======================
    #! /usr/bin/perl -w

    use strict;
    use warnings;

    my $destdir = shift or die "ugh: $!";

    my $shcmd =<<NED;
    for file in *
    do
    if [ -f \$file ]
    then
    cat \$file >> $destdir/\$file
    fi
    done
    NED

    print $shcmd;

    exec $shcmd;
    =8<======================

    --
    Tsu|
    Do|
    Nimh|
    :) |
    Tsu Do Nimh, May 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    0
    Views:
    338
    Xah Lee
    Oct 20, 2005
  2. Tom Lynch
    Replies:
    2
    Views:
    2,014
    Tom Lynch
    Oct 12, 2007
  3. Mark
    Replies:
    8
    Views:
    88
    Tad McClellan
    Apr 29, 2004
  4. Larry
    Replies:
    1
    Views:
    95
    Martien Verbruggen
    Feb 3, 2005
  5. Oxnard
    Replies:
    13
    Views:
    206
    Fabian Pilkowski
    Jun 15, 2005
Loading...

Share This Page