grep problems in Linux (fedora 3)

Discussion in 'Tech Talk' started by lomfs24, Apr 10, 2005.

  1. lomfs24

    lomfs24

    Messages:
    2,388
    Likes Received:
    144
    Joined:
    Apr 19, 2003
    Location:
    Montana
    I have a problem. I am trying to grep a file in Linux and pull a certain word.

    Here is my command
    cat messages | grep connect

    And I want to get all the lines that have the word connect in it but I do not want lines that have the word disconnect in it. How would I modify that line to get only what I want?
     
  2. lomfs24

    lomfs24

    Messages:
    2,388
    Likes Received:
    144
    Joined:
    Apr 19, 2003
    Location:
    Montana
    Never mind, I got it. The -w option seemed to do the trick
     

  3. Dandapani

    Dandapani

    Messages:
    8,216
    Likes Received:
    13
    Joined:
    Mar 24, 2004
    Location:
    Gulf side Florida
    glad you found it.

    an alternative technique is using a pair of greps:

    cat messages | grep connect | grep -v disconnect
     
  4. Sinister Angel

    Sinister Angel I'd Hit It!

    Messages:
    252
    Likes Received:
    0
    Joined:
    Oct 11, 2004
    Location:
    Traverse City, Michigan
    You could also do it another way depending on what style of regular expressions grep uses..
     
  5. lomfs24

    lomfs24

    Messages:
    2,388
    Likes Received:
    144
    Joined:
    Apr 19, 2003
    Location:
    Montana
    Thanks for the help dmobrien2001. Your way works too.

    Here's what I am really trying to do. I helped put together an IRLP server for the local ham radio club. With a radio you can connect to hundreds of places in the world. And those places can connect to your repeater. Each time someone connects, either inbound or outbound, it adds a line in the logs. The problem is that logs are unruley because there is a lot of other stuff logged that no one cares about. Here is a sample.
    _____________________________________________________


    Apr 10 2005 11:30:01 -0600 getWxRpt: Get METAR for KBZN
    Apr 10 2005 11:30:09 -0600 getWxRpt: Got METAR for KBZN
    Apr 10 2005 11:30:20 -0600 getWxRpt: /home/irlp/audio/custom/wx/KBZN.ul has been saved
    Apr 10 2005 11:30:20 -0600 getWxRpt: Get METAR for KLVM
    Apr 10 2005 11:30:21 -0600 getWxRpt: Got METAR for KLVM
    Apr 10 2005 11:30:36 -0600 getWxRpt: /home/irlp/audio/custom/wx/KLVM.ul has been saved
    Apr 10 2005 11:32:21 -0600 stn5092 connect ADPCM
    Apr 10 2005 11:37:25 -0600 stn5092 disconnect
    Apr 10 2005 11:41:19 -0600 decode: DTMF = 7430
    Apr 10 2005 11:41:19 -0600 IRLP call initiated to stn7430
    Apr 10 2005 11:41:22 -0600 stn743 reply connectok ADPCM
    Apr 10 2005 11:45:01 -0600 getWxRpt: Get METAR for KBZN
    Apr 10 2005 11:45:03 -0600 getWxRpt: Got METAR for KBZN
    Apr 10 2005 11:45:14 -0600 getWxRpt: /home/irlp/audio/custom/wx/KBZN.ul has been saved
    Apr 10 2005 11:45:14 -0600 getWxRpt: Get METAR for KLVM
    Apr 10 2005 11:45:18 -0600 getWxRpt: Got METAR for KLVM
    Apr 10 2005 11:45:32 -0600 getWxRpt: /home/irlp/audio/custom/wx/KLVM.ul has been saved
    Apr 10 2005 11:48:52 -0600 decode: DTMF = 73
    Apr 10 2005 11:48:52 -0600 Node Disconnect from stn7430
    Apr 10 2005 12:00:01 -0600 getWxRpt: Get METAR for KBZN
    Apr 10 2005 12:00:03 -0600 getWxRpt: Got METAR for KBZN
    Apr 10 2005 12:00:14 -0600 getWxRpt: /home/irlp/audio/custom/wx/KBZN.ul has been saved
    Apr 10 2005 12:00:14 -0600 getWxRpt: Get METAR for KLVM
    Apr 10 2005 12:00:16 -0600 getWxRpt: Got METAR for KLVM
    Apr 10 2005 12:00:28 -0600 getWxRpt: /home/irlp/audio/custom/wx/KLVM.ul has been saved
    Apr 10 2005 12:15:02 -0600 getWxRpt: Get METAR for KBZN
    Apr 10 2005 12:15:03 -0600 getWxRpt: Got METAR for KBZN
    Apr 10 2005 12:15:14 -0600 getWxRpt: /home/irlp/audio/custom/wx/KBZN.ul has been saved
    Apr 10 2005 12:15:14 -0600 getWxRpt: Get METAR for KLVM
    Apr 10 2005 12:15:16 -0600 getWxRpt: Got METAR for KLVM
    Apr 10 2005 12:15:29 -0600 getWxRpt: /home/irlp/audio/custom/wx/KLVM.ul has been saved
    Apr 10 2005 12:17:10 -0600 stn5092 connect ADPCM
    Apr 10 2005 12:19:12 -0600 stn5092 disconnect

    __________________________________________________________
    And I want to only extract the date, time and station that connected along with the time. So I want it to look like this.
    ___________________________________________________________

    Apr 09 2005 21:08:35 -0600 stn743 reply connectok ADPCM
    Apr 09 2005 21:11:20 -0600 stn3568 reply connectok ADPCM
    Apr 09 2005 21:14:05 -0600 stn3568 connect ADPCM
    Apr 09 2005 21:26:47 -0600 stn743 reply connectok ADPCM
    Apr 10 2005 02:21:45 -0600 stn5092 connect ADPCM
    Apr 10 2005 08:15:43 -0600 stn743 reply connectok ADPCM
    Apr 10 2005 10:10:58 -0600 stn3568 reply connectok ADPCM
    Apr 10 2005 11:14:02 -0600 stn743 reply connectok ADPCM
    Apr 10 2005 11:32:21 -0600 stn5092 connect ADPCM
    Apr 10 2005 11:41:22 -0600 stn743 reply connectok ADPCM
    Apr 10 2005 12:17:10 -0600 stn5092 connect ADPCM
    Apr 10 2005 13:15:11 -0600 stn3568 reply connectok ADPCM
    Apr 10 2005 13:33:57 -0600 stn3568 connect ADPCM
    Apr 10 2005 14:04:54 -0600 stn3568 reply connectok ADPCM
    Apr 10 2005 15:48:21 -0600 stn5092 connect ADPCM
    Apr 10 2005 16:46:09 -0600 Node Connected to reflector ref9250 on channel 0
    [repeater@localhost ~]
    _________________________________________________________

    But there is a lot of that that I would also like to strip out. The end goal is a dynamic website, probably written in php, that when you went to that page would parse the log file and display the date, time and station for each connection for the last 30 days. Would be a neat addition to the local ham club site.
     
  6. HerrGlock

    HerrGlock Scouts Out CLM

    Messages:
    23,804
    Likes Received:
    264
    Joined:
    Dec 28, 2000
    If you just wanted the connecok lines this would do it in a readable format:

    grep connectok messages|awk '{print "On "$1,$2,$3" At "$4", "$6" Received "$8}'


    Get rid of the cat, you do not need it.

    This will get you just the raw data with connect only:
    grep connect messages|grep -v disconnect

    DanH
     
  7. frefoo

    frefoo

    Messages:
    174
    Likes Received:
    0
    Joined:
    Apr 4, 2004
    Location:
    Northen VA
    I think the phrase is...

    (With UNIX) There is always more then 1 way to do things :)

    Just joking...

    How about a perl script?

    :)
     
  8. lomfs24

    lomfs24

    Messages:
    2,388
    Likes Received:
    144
    Joined:
    Apr 19, 2003
    Location:
    Montana
    I am kinda handy with writing my own stuff but I will be the first to say, there is a lot I need to learn. It will probably eventually be morphed into another language. But for now I just needed to know how to extract the data that I really want.
     
  9. frefoo

    frefoo

    Messages:
    174
    Likes Received:
    0
    Joined:
    Apr 4, 2004
    Location:
    Northen VA
    My point exactly

    "But for now I just needed to know how to extract the data that I really want."

    How many people gave you differet ways to give you the data you need using different methods and different ways to get what you want?