Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2003
    Posts
    4

    Exclamation Unanswered: AWK Substring Error

    Hi,

    I am running a small awk script to get the substring of
    a string. But I am getting "invalid character in expression"
    error. I played around with all single quotes and double
    quotes but I could not figure it out.

    Here is my script
    File Name : test
    -------------------------------
    #!/bin/bash
    echo $1
    HEADER=`head -n 1 $1`
    echo Header is $HEADER
    HEADERDATE=`awk "BEGIN {print substr('$HEADER',1,5)}"`
    echo $HEADERDATE

    My Error
    -------------------------------

    [oracle@ODW3 unifi]$ ./test source*/wrapmainUPD.txt
    sourcefiles/wrapmainUPD.txt
    Header is HDR, Master Table Data, Date [9,10,2003]
    awk: cmd. line:1: BEGIN {print substr('HDR, Master Table Data, Date [9,10,2003]',1,5)}
    awk: cmd. line:1: ^ invalid char ''' in expression



    Any help is greatly appreciated.


    Thanks,
    Modem

  2. #2
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hello smodem,
    try this :

    #!/bin/bash
    echo $1
    HEADERDATE=`head –n 1 $1 | awk '{
    dummy=substr($1, 1, 5)
    print (dummy)
    }'`
    echo $HEADERDATE

    Greetings from Germany
    Peter F.

  3. #3
    Join Date
    Sep 2003
    Posts
    4

    Exclamation AWK Substring

    Hi Peter,

    Thanks for the solution. The script is not getting the complete string. It is getting only 4 characters. This is the output.

    Script
    ---------------------------
    #!/bin/bash
    echo $1

    HEADERDATE=`head -n 1 $1 | awk '{dummy=substr($1,1,10); print(dummy)}'`
    echo Header Date is $HEADERDATE
    HEADERLENGTH=`head -n 1 $1 | awk '{dummy=length($1); print(dummy)}'`
    echo Header Length is $HEADERLENGTH


    Output
    ----------------------

    $ ./test1 source*/wrapmainUPD.txt
    sourcefiles/wrapmainUPD.txt
    Header Date HDR,
    Header Length is 4

    Thanks,
    Modem.

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    gelöscht
    Last edited by fla5do; 10-03-03 at 07:54.

Posting Permissions

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