awk overwriting beginning of line! - awk

This is a discussion on awk overwriting beginning of line! - awk ; [Sorry if I posted an incomplete message before. I erroneously pressed the mouse key on the post button.] Hello Everybody, I was trying to teach myself some awk. I wrote this small awk script: ========================================== #!/usr/bin/env gawk #Original text #2 ...

+ Reply to Thread
Results 1 to 4 of 4

awk overwriting beginning of line!

  1. Default awk overwriting beginning of line!

    [Sorry if I posted an incomplete message before. I erroneously
    pressed the mouse key on the post button.]

    Hello Everybody,

    I was trying to teach myself some awk. I wrote this small awk script:
    ==========================================
    #!/usr/bin/env gawk

    #Original text
    #2 7/26/1997 0:00 10 Active 1 7/26/1997
    0:00 [NULL]
    #4 7/30/2000 0:00 10 Active 1 6/16/2001
    0:00 [NULL]

    #Modified text
    #INSERT INTO TABLENAME VALUES(2,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    HH:MI'),10,'Active',1,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    HH:MI'),NULL);
    #INSERT INTO TABLENAME VALUES(4,TO_DATE('7/30/2000 0:00','MM/DD/YYYY
    HH:MI'),10,'Active',1,TO_DATE('6/16/2001 0:00','MM/DD/YYYY
    HH:MI'),NULL);

    {
    print "" > "santanu.out";
    a=$9; gsub(/\[|\]/, "", a);
    printf("INSERT INTO TABLENAME VALUES(");
    printf($1",TO_DATE('"$2" "$3"','MM/DD/YYYY HH:MI'),");
    printf($4",'"$5"',"$6",TO_DATE('"$7" "$8"','MM/DD/YYYY HH:MI'),");
    printf(a);
    printf(");");
    printf("\n");
    }
    ====================================================

    But when I run this script, on a file containing the "Original text",
    instead
    of getting the "Modified text", I get this:
    ==================================
    $awk -f santanu.awk santanu.orig
    );SERT INTO TABLENAME VALUES(2,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    HH:MI'),10,'Active',1,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    HH:MI'),NULL
    );SERT INTO TABLENAME VALUES(4,TO_DATE('7/30/2000 0:00','MM/DD/YYYY
    HH:MI'),10,'Active',1,TO_DATE('6/6/2001 0:00','MM/DD/YYYY HH:MI'),NULL
    ==================================

    Please note that the lines start with ");SERT" instead of "INSERT",
    whereas
    the ");" part should be at the end!

    What is the reason for this? I am baffled!

    Regards,
    Santanu


  2. Default Re: awk overwriting beginning of line!

    Oh forget it. I just now realized that the input file 'santanu.orig'
    was
    created in Windows. So, I converted it to unix format using dos2unix.
    After that, the script worked as expected.


  3. Default Re: awk overwriting beginning of line!

    santanu wrote:
    > [Sorry if I posted an incomplete message before. I erroneously
    > pressed the mouse key on the post button.]
    >
    > Hello Everybody,
    >
    > I was trying to teach myself some awk. I wrote this small awk script:
    > ==========================================
    > #!/usr/bin/env gawk
    >
    > #Original text
    > #2 7/26/1997 0:00 10 Active 1 7/26/1997
    > 0:00 [NULL]
    > #4 7/30/2000 0:00 10 Active 1 6/16/2001
    > 0:00 [NULL]
    >
    > #Modified text
    > #INSERT INTO TABLENAME VALUES(2,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    > HH:MI'),10,'Active',1,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    > HH:MI'),NULL);
    > #INSERT INTO TABLENAME VALUES(4,TO_DATE('7/30/2000 0:00','MM/DD/YYYY
    > HH:MI'),10,'Active',1,TO_DATE('6/16/2001 0:00','MM/DD/YYYY
    > HH:MI'),NULL);
    >
    > {
    > print "" > "santanu.out";
    > a=$9; gsub(/\[|\]/, "", a);
    > printf("INSERT INTO TABLENAME VALUES(");
    > printf($1",TO_DATE('"$2" "$3"','MM/DD/YYYY HH:MI'),");
    > printf($4",'"$5"',"$6",TO_DATE('"$7" "$8"','MM/DD/YYYY HH:MI'),");
    > printf(a);
    > printf(");");
    > printf("\n");
    > }
    > ====================================================
    >
    > But when I run this script, on a file containing the "Original text",
    > instead
    > of getting the "Modified text", I get this:
    > ==================================
    > $awk -f santanu.awk santanu.orig
    > );SERT INTO TABLENAME VALUES(2,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    > HH:MI'),10,'Active',1,TO_DATE('7/26/1997 0:00','MM/DD/YYYY
    > HH:MI'),NULL
    > );SERT INTO TABLENAME VALUES(4,TO_DATE('7/30/2000 0:00','MM/DD/YYYY
    > HH:MI'),10,'Active',1,TO_DATE('6/6/2001 0:00','MM/DD/YYYY HH:MI'),NULL
    > ==================================
    >
    > Please note that the lines start with ");SERT" instead of "INSERT",
    > whereas
    > the ");" part should be at the end!
    >
    > What is the reason for this? I am baffled!
    >
    > Regards,
    > Santanu
    >


    Your input file probably was created on Windows or some such and has
    spurious control characters at the end of the line so that when you do this:

    printf(a);

    you're printing a control character that's jumping back back to the
    beginning of your output line. To test that, comment out that line and
    your lines will start with INSERT. Run dos2unix or some such conversion
    program to fix the input file.

    Now, for the script - among other things, you misunderstand how to use
    printf. The first argument for printf is a format string, the second and
    subsequent are the data to be printed. By trying to print the data
    without a succeeding formatting string, you're openign yourself up to
    all sorts of weird behavior if your input data contains formatting
    strings (e.g. "%s").

    The above should be rewitten as:

    {
    gsub(/[][]/, "", $9)
    printf("INSERT INTO TABLENAME VALUES(%s,TO_DATE('%s %s','MM/DD/YYYY
    HH:MI'),%s,'%s',%s,TO_DATE('%s %s','MM/DD/YYYY HH:MI'),%s);\n",
    $1,$2,$3,$4,$5,$7,$8,$9)
    }

    Regards,

    Ed.

  4. Default Re: awk overwriting beginning of line!

    On Jun 5, 2:23 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
    > Your input file probably was created on Windows or some such and has
    > spurious control characters at the end of the line so that when you do this:
    >
    > printf(a);
    >
    > you're printing a control character that's jumping back back to the
    > beginning of your output line. To test that, comment out that line and
    > your lines will start with INSERT. Run dos2unix or some such conversion
    > program to fix the input file.


    You are right. I just figured that one out sometime after my original
    posting.

    > Now, for the script - among other things, you misunderstand how to use
    > printf. The first argument for printf is a format string, the second and
    > subsequent are the data to be printed. By trying to print the data
    > without a succeeding formatting string, you're openign yourself up to
    > all sorts of weird behavior if your input data contains formatting
    > strings (e.g. "%s").
    >
    > The above should be rewitten as:
    >
    > {
    > gsub(/[][]/, "", $9)
    > printf("INSERT INTO TABLENAME VALUES(%s,TO_DATE('%s %s','MM/DD/YYYY
    > HH:MI'),%s,'%s',%s,TO_DATE('%s %s','MM/DD/YYYY HH:MI'),%s);\n",
    > $1,$2,$3,$4,$5,$7,$8,$9)


    Thanks for the tip. I did not realize that the $n variables would
    replace %s in the
    format strings. (First I tried things like "INSERT $1" expecting awk
    to
    work like perl, but that did not happen.)

    Regards,
    Santanu


+ Reply to Thread

Similar Threads

  1. Compare a token from beginning of the line.
    By Application Development in forum awk
    Replies: 2
    Last Post: 10-24-2007, 02:05 AM
  2. ( at the beginning of a line in docstring
    By Application Development in forum lisp
    Replies: 4
    Last Post: 07-23-2007, 10:25 AM
  3. awk overwriting beginning of line!
    By Application Development in forum awk
    Replies: 0
    Last Post: 06-04-2007, 03:46 PM
  4. Furthest char from the beginning of line
    By Application Development in forum awk
    Replies: 10
    Last Post: 09-07-2006, 06:19 AM
  5. Add content at beginning / end of the line in VIM?
    By Application Development in forum Editors
    Replies: 4
    Last Post: 09-01-2006, 11:03 AM