Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
hi guys! im trying to use Unix and Lex to character match. basically i have a log file that contains the following: [31/Jan/2006:23:45:57 962 [31/Jan/2006:23:45:57 893 [31/Jan/2006:23:45:57 827 [31/Jan/2006:23:45:57 2766 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241

    Question Unix Character matchin.


    hi guys!

    im trying to use Unix and Lex to character match. basically i have a log file that contains the following:

    [31/Jan/2006:23:45:57 962
    [31/Jan/2006:23:45:57 893
    [31/Jan/2006:23:45:57 827
    [31/Jan/2006:23:45:57 2766
    [31/Jan/2006:23:45:57 2017
    [31/Jan/2006:23:45:57 10991

    i would like to use lex to achieve the answer:

    31/Jan/2006 962
    31/Jan/2006 893
    31/Jan/2006 827
    31/Jan/2006 2766

    basically removing the time stamp from the date, but leaving all other characters

    i have tried many ways, using lex, and awk - but cant seem to find an answer.

    i would really appreciate anyone's help.

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    One option:
    Code:
    [hector@centos1 ~]$ echo '[31/Jan/2006:23:45:57 962' | awk '{ sub(/\[/,"") ; sub (/:.*/,"",$1) ; print $0 }'
    31/Jan/2006 962

  3. #3
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    Thanks for the reply

    But strangely i get a syntax error:

    awk: syntax error near line 1
    awk: illegal statement near line 1
    awk: syntax error near line 1
    awk: illegal statement near line 1

    Dont know whats going wrong.

  4. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Did you copy and paste that example exactly?

    In your case you need to do:
    Code:
    $ awk '{ sub(/\[/,"") ; sub (/:.*/,"",$1) ; print $0 }' log-file-here
    If you're still getting errors post the exact errors. (Just take a copy of the whole session including the command you typed.)

  5. #5
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    Quote Originally Posted by anomie
    Did you copy and paste that example exactly?

    In your case you need to do:
    Code:
    $ awk '{ sub(/\[/,"") ; sub (/:.*/,"",$1) ; print $0 }' log-file-here
    If you're still getting errors post the exact errors. (Just take a copy of the whole session including the command you typed.)


    i put the following code into a file names soln09awktime:

    Code:
    $ awk '{ sub(/\[/,"") ; sub (/:.*/,"",$1) ; print $0 }' soln09log
    soln09log being the name of the log file im trying to manipulate.

    then in the Unix to run the file im typing:

    Code:
    $ soln09awktime

    i get the same error:

    Code:
    awk: syntax error near line 1
    awk: illegal statement near line 1
    awk: syntax error near line 1
    awk: illegal statement near line 1

  6. #6
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    Could it be the word "sub"

    what does this actually mean?

  7. #7
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    What distro are you seeing this error on? Also, what does awk --version | head -1 show you?

    I thought all GNU/Linux used gawk (and awk is a symlink to it). This should work no problem. Try running it directly from the command line -- you don't need to make it into a script.

    All this is doing is squeezing out the '[' character, and then squeezing out everything from ':' until the end of the word in the first column.

    If you want to test whether the sub() function works on your awk implementation, just try:
    Code:
    $ echo 'Moo' | awk '{ sub(/M/,"B"); print }'
    Any complaints about the syntax? It should have printed Boo.

  8. #8
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    im running it on a UNIX server over PuTTY.

    i still get an error, so i assume sub doesnt work.

    login1[u] echo 'Moo' | awk '{ sub(/M/,"B"); print }'
    awk: syntax error near line 1
    awk: illegal statement near line 1

  9. #9
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    is there a way of doing

    Code:
    $ awk '{ sub(/\[/,"") ; sub (/:.*/,"",$1) ; print $0 }' log-file-here
    using LEX, maybe this would avoid the error.

  10. #10
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    You didn't state which version / awk you're using. If it's a SunOS box it could be nawk. Unfortunately different implementations of awk sometimes have different subtleties to their syntax.

    I'm not interested in learning lex just to answer one question, so I will go away now. Good luck.

Page 1 of 2 1 2 LastLast

Posting Permissions

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