PHP scripting... An similiar awk command

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Smith

    PHP scripting... An similiar awk command

    Greetings,

    I have the need to take a file and remove the trailing white space as
    well as any empty lines... I inherited this script:


    cat $1 | awk -v quote="'" \
    '
    /^[ ]*$/ { next; }
    /^.*[ ]*$/ {
    printf ("%s\n",subs tr ($0,1,match($0, "[
    ]*$")-1));
    next;
    }
    { print $0; }
    '


    I prefer to do it in PHP. Is there a simpler way?

  • Tim Roberts

    #2
    Re: PHP scripting... An similiar awk command

    "Anthony Smith" <mrsmithq@hotma il.comwrote:
    >
    >I have the need to take a file and remove the trailing white space as
    >well as any empty lines... I inherited this script:
    >>
    >cat $1 | awk -v quote="'" \
    '
    /^[ ]*$/ { next; }
    /^.*[ ]*$/ {
    printf ("%s\n",subs tr ($0,1,match($0, "[
    >]*$")-1));
    next;
    }
    { print $0; }
    '
    >I prefer to do it in PHP. Is there a simpler way?
    Why would you prefer to do it in PHP? This is exactly what commands like
    perl and awk and sed were designed for, not php.

    There are, however, simpler ways to do it. For example, this should
    replace your entire script:

    sed -e 's/[ \t]*$//' -e '/^$/d' $1
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    • Anthony Smith

      #3
      Re: PHP scripting... An similiar awk command

      Excellent point Tim.

      Well, our web development (I just started in this group) consists of
      perl and python in cgi. I want to move to php. Unfortunately, the idea
      was to consolidate, but instead I am adding. However when I saw you
      could do scripting with PHP I thought I had an answer for Perl and
      Python. I do realize that there are some things that Perl and/or Python
      just do better. Perl seems to intimidate people and become hard to read
      past a certain point. I do understand that part of that is the
      programmer's fault. I have no problem with Perl. I just thought since
      we are using PHP to do web development why not use it as our scripting
      language too. Since sed is not perl, this should be ok.


      Tim Roberts wrote:
      "Anthony Smith" <mrsmithq@hotma il.comwrote:

      I have the need to take a file and remove the trailing white space as
      well as any empty lines... I inherited this script:
      >
      cat $1 | awk -v quote="'" \
      '
      /^[ ]*$/ { next; }
      /^.*[ ]*$/ {
      printf ("%s\n",subs tr ($0,1,match($0, "[
      ]*$")-1));
      next;
      }
      { print $0; }
      '
      I prefer to do it in PHP. Is there a simpler way?
      >
      Why would you prefer to do it in PHP? This is exactly what commands like
      perl and awk and sed were designed for, not php.
      >
      There are, however, simpler ways to do it. For example, this should
      replace your entire script:
      >
      sed -e 's/[ \t]*$//' -e '/^$/d' $1
      --
      Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • Chung Leong

        #4
        Re: PHP scripting... An similiar awk command


        Anthony Smith wrote:
        Greetings,
        >
        I have the need to take a file and remove the trailing white space as
        well as any empty lines... I inherited this script:
        >
        >
        cat $1 | awk -v quote="'" \
        '
        /^[ ]*$/ { next; }
        /^.*[ ]*$/ {
        printf ("%s\n",subs tr ($0,1,match($0, "[
        ]*$")-1));
        next;
        }
        { print $0; }
        '
        >
        >
        I prefer to do it in PHP. Is there a simpler way?
        $a = file($filename) ;
        $a = array_filter(ar ray_map('rtrim' , $a));

        Comment

        Working...