Professional Web Applications Themes

Deleting # and other characters - PERL Beginners

I need to remove ./ and #from a list of files. I can do it in sed but I am not able to use it in my perl script. I tried to do something like this chomp ($txtlist = <STDIN>); qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a # qx' sed -e"s?\([ /]\)\./?

?g" $txtlist; # To remove lines starting with a ../ I can do it if i hard code the file name but if i try to use $txtlist it does not work. What am i doing wrong. Thanks Raghu __________________________________________________ _______________ Want to check ...

  1. #1

    Default Deleting # and other characters

    I need to remove ./ and #from a list of files. I can do it in sed but I am
    not able to use it in my perl script. I tried to do something like this

    chomp ($txtlist = <STDIN>);
    qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a #
    qx' sed -e"s?\([ /]\)\./?\1?g" $txtlist; # To remove lines starting with a
    ../

    I can do it if i hard code the file name but if i try to use $txtlist it
    does not work. What am i doing wrong.

    Thanks

    Raghu

    __________________________________________________ _______________
    Want to check if your PC is virus-infected? Get a FREE computer virus scan
    online from McAfee.
    [url]http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963[/url]

    Raghu Murthy Guest

  2. #2

    Default Re: Deleting # and other characters

    In article <BAY10-F34hs0u9kLEc9000255ebhotmail.com>, Raghu Murthy wrote:
    > I need to remove ./ and #from a list of files. I can do it in sed but I am
    > not able to use it in my perl script. I tried to do something like this
    >
    > chomp ($txtlist = <STDIN>);
    > qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a #
    > qx' sed -e"s?\([ /]\)\./?\1?g" $txtlist; # To remove lines starting with
    > a ./
    >
    > I can do it if i hard code the file name but if i try to use $txtlist it
    > does not work. What am i doing wrong.
    Can't you just do something like:

    while (<DATA>) {
    s|^#+\s*||; # remove leading #s and any spaces
    s|^\./||; # remove leading "./"
    print;
    }

    To remove the entire line add "next if " in front of the regex.

    -K


    --
    Kevin Pfeiffer

    Kevin Pfeiffer Guest

  3. #3

    Default Re: Deleting # and other characters

    In article <2410384.iuVzuSzG49sputnik.tiros.net>, Kevin Pfeiffer wrote:
    > In article <BAY10-F34hs0u9kLEc9000255ebhotmail.com>, Raghu Murthy wrote:
    >
    >> I need to remove ./ and #from a list of files. I can do it in sed but I
    >> am not able to use it in my perl script. I tried to do something like
    >> this
    >>
    >> chomp ($txtlist = <STDIN>);
    >> qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a #
    >> qx' sed -e"s?\([ /]\)\./?\1?g" $txtlist; # To remove lines starting with
    >> a ./
    >>
    >> I can do it if i hard code the file name but if i try to use $txtlist it
    >> does not work. What am i doing wrong.
    Thinking that I misunderstood the question... $txtlist is the file you want
    to edit?

    If you call the script so: ./strip_chars txtlistfile > new_version

    then you would start the code I posted with "while (<>) {" instead.


    --
    Kevin Pfeiffer

    Kevin Pfeiffer Guest

  4. #4

    Default Deleting # and other characters

    I tried using
    next if s?\([ /]\)\./?\1?g;

    and it did not work. The file looks like this

    a b ./zyc/dfdk ./dkld/kdj
    k l ./ksdk/ksk/ksd ./kskd/kdsk

    Thanks

    __________________________________________________ _______________
    Never get a busy signal because you are always connected with high-speed
    Internet access. Click here to comparison-shop providers.
    [url]https://broadband.msn.com[/url]

    Raghu Murthy Guest

  5. #5

    Default Re: Deleting # and other characters

    I tried doing the following

    next if s?\([ /]\)\./?\1?g;

    For some reason it is not removing the ./ from the file. Any suggestions are
    welcome.
    The file is in this format

    a b ./dsfj/dfl/dksl ./ksdfl/dsld

    c d ./sds/dsl/dksld ./kdf/ksd/ksdk

    Thanks

    Raghu

    In article <2410384.iuVzuSzG49sputnik.tiros.net>, Kevin Pfeiffer wrote:
    >In article <BAY10-F34hs0u9kLEc9000255ebhotmail.com>, Raghu Murthy wrote:
    >
    >>I need to remove ./ and #from a list of files. I can do it in sed but I
    >>am not able to use it in my perl script. I tried to do something like
    >>this
    >>
    >>chomp ($txtlist = <STDIN>);
    >>qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a #
    >>qx' sed -e"s?\([ /]\)\./?\1?g" $txtlist; # To remove lines starting with
    >>a ./
    >>
    >>I can do it if i hard code the file name but if i try to use $txtlist it
    >>does not work. What am i doing wrong.
    Thinking that I misunderstood the question... $txtlist is the file you want
    to edit?

    If you call the script so: ./strip_chars txtlistfile > new_version

    then you would start the code I posted with "while (<>) {" instead.


    --
    Kevin Pfeiffer

    __________________________________________________ _______________
    Fretting that your Hotmail account may expire because you forgot to sign in
    enough? Get Hotmail Extra Storage today!
    [url]http://join.msn.com/?PAGE=features/es[/url]

    Raghu Murthy Guest

  6. #6

    Default Re: Deleting # and other characters

    In article <BAY10-F15HvoFOAmEaJ00024af8hotmail.com>, Raghu Murthy wrote:
    > I tried doing the following
    >
    > next if s?\([ /]\)\./?\1?g;
    That looks to me more like sed (as best I can tell) and not Perl.
    > For some reason it is not removing the ./ from the file. Any suggestions
    > are welcome.
    > The file is in this format
    >
    > a b ./dsfj/dfl/dksl ./ksdfl/dsld
    >
    > c d ./sds/dsl/dksld ./kdf/ksd/ksdk
    Well, you could always use:

    tr/\.\///d;
    print;

    ....which would give you this:
    a b dsfjdfldksl ksdfldsld

    c d sdsdsldksld kdfksdksdk


    Or this:
    s|\./||g;
    print;

    ....which produces this:
    a b dsfj/dfl/dksl ksdfl/dsld

    c d sds/dsl/dksld kdf/ksd/ksdk

    ....if that is what you want (I'm not quite sure).

    -Kevin


    --
    Kevin Pfeiffer

    Kevin Pfeiffer Guest

  7. #7

    Default Re: Deleting # and other characters

    Raghu Murthy wrote:
    >
    > I need to remove ./ and #from a list of files. I can do it in sed but I am
    > not able to use it in my perl script. I tried to do something like this
    >
    > chomp ($txtlist = <STDIN>);
    > qx' sed -e "/^#/d $txtlist'; # To remove lines starting with a #
    > qx' sed -e"s?\([ /]\)\./?\1?g" $txtlist; # To remove lines starting with a
    > ./
    >
    > I can do it if i hard code the file name but if i try to use $txtlist it
    > does not work. What am i doing wrong.
    Perl comes with a handy utility called s2p (sed to perl) that will help
    you convert sed scripts to perl. $txtlist does not work because qx runs
    the sed program and returns the results to your perl program but you are
    not doing anything with the results.

    You can do it in perl something like this:

    perl -i~ -pe's|^#||; s|([ /])\./|$1|g' file1 file2 file3 ... fileN


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  8. #8

    Default Re: Deleting # and other characters

    > > I need to remove ./ and #from a list of files. I can do it in sed but I am
    > > not able to use it in my perl script. I tried to do something like this
    # delete all .s and /s
    $variable_to_remove_from =~ tr/\.\///d;
    # remove the first ./ and anything before it from
    # $variable_to_remove_from
    if ($variable_to_remove_from =~ m/\.\//) {
    $variable_to_remove_from =~ m/\.\//;
    $variable_to_remove_from = $';
    }
    # remove only ./ from the file if it exists and is on a word boundary
    if ($variable_to_remove_from =~ m/\b\.\//) {
    $variable_to_remove_from =~ m/\b\.\//;
    $variable_to_remove_from = $';
    }


    Dan Anderson Guest

Similar Threads

  1. Replies: 1
    Last Post: August 12th, 03:28 PM
  2. [PHP] deleting one cookie is deleting both..why? (tiny scripts)
    By Chris Shiflett in forum PHP Development
    Replies: 0
    Last Post: July 17th, 05:50 PM
  3. Replies: 0
    Last Post: July 17th, 12:54 PM
  4. Replies: 3
    Last Post: July 9th, 10:25 AM
  5. Deleting ICS
    By Ray in forum Windows Networking
    Replies: 1
    Last Post: July 3rd, 12:13 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