some help using grep

Discussion in 'OT Technology' started by kronik85, Jun 26, 2007.

  1. kronik85

    kronik85 New Member

    Joined:
    Feb 8, 2005
    Messages:
    34,837
    Likes Received:
    0
    Location:
    Deutschland
    so my boss has me automating a bunch of things for some dept in the company and i'm using ksh to script it (never written anything in a shell script). i'm having trouble getting grep to identify a certain string in some output files. namely with the *wildcard.

    String to match :

    Uploading 06.26.2007.19:41:00.zip to /export/home/someuser/mti/ftp/06.26.2007.19:41:00.zip

    Command so far :

    grep -c "Uploading ${datestamp}.zip to *.zip" script.log


    ${datestamp} matches "06.26.2007.19:41:00"

    grep -c "Uploading ${datestamp}.zip to " script.log works perfectly, however I need the directory (/export...ftp/) to be a wildcard since the script might be writing to several different places. I've tried to use \\ around it, before it, since I know that sometimes reserved characters evaluate differently and such. But to no avail.
     
  2. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    Are there other lines within the log fie that have the word "Uploading"

    If not, why not just grep for the word "Uploading".

    No need to over complicate things....
     
  3. kronik85

    kronik85 New Member

    Joined:
    Feb 8, 2005
    Messages:
    34,837
    Likes Received:
    0
    Location:
    Deutschland
    it's a log of all the script's activities over a long period of time/continual use, so ya, it should end up with plenty of 'Uploading's. However I'm wondering how I can make the sftp transfer fail so that it attempts the upload, but a problem is encountered. Just to see what that output looks like and make sure I don't accidently find the string "Uploading blah.zip *.zip" only to have it be followed with a message like "Upload failed" only I never picked that up with grep.
     
  4. RiSk

    RiSk New Member

    Joined:
    Jun 8, 2004
    Messages:
    6,564
    Likes Received:
    0
    Location:
    Irving, Texas
    okay here is the deal.... Show me what the message file that your greping looks like....and then type out what you want it to look like and ill make a grep string for ya :p
     
  5. kronik85

    kronik85 New Member

    Joined:
    Feb 8, 2005
    Messages:
    34,837
    Likes Received:
    0
    Location:
    Deutschland
    sounds more like something for sed, string replacement?

    i simply need to pattern match the string above, make sure there's at least once instance, but i'm not at work atm (came home). but the string i'm looking for is the one above, and i just need it to wildcard that directory reference. any help is :bowdown:
     
  6. crontab

    crontab (uid = 0)

    Joined:
    Nov 14, 2000
    Messages:
    23,446
    Likes Received:
    12
    I'm still not understanding what you're looking for. So you want to grep out all the instances where it starts to upload a file but don't want to see the failures?

    If your log states "Uploading" and fails states "Upload", then grepping for Uploading won't report the failures. But do you want that as well or not? Because that grep command you're using won't catch it.

    To answer you original question, the grep command you should use is:

    grep "Uploading ${datestamp}.zip to .*.zip" zzz

    Code:
    [[email protected] ~]$ cat zzz
    Uploading 06.26.2007.19:41:00.zip to /export/home/someuser/mti/ftp/06.26.2007.19:41:00.zip
    Uploading huh
    asdfads
    asdfasdf
    [[email protected] ~]$ export datestamp=06.26.2007.19:41:00
    [[email protected] ~]$ echo ${datestamp}
    06.26.2007.19:41:00
    [[email protected] ~]$ grep "Uploading" zzz
    Uploading 06.26.2007.19:41:00.zip to /export/home/someuser/mti/ftp/06.26.2007.19:41:00.zip
    Uploading huh
    [[email protected] ~]$ grep "Uploading ${datestamp}.zip to *.zip" zzz
    [[email protected] ~]$ grep "Uploading ${datestamp}.zip to .*.zip" zzz
    Uploading 06.26.2007.19:41:00.zip to /export/home/someuser/mti/ftp/06.26.2007.19:41:00.zip
    [[email protected] ~]$ 
    
    grep interprets the expression " .* " to match zero or more occurrences of any character. The "." represents any character.

    As Risk stated, post the log and which lines you want. You seem to be doing more work than needed...
     
  7. kronik85

    kronik85 New Member

    Joined:
    Feb 8, 2005
    Messages:
    34,837
    Likes Received:
    0
    Location:
    Deutschland
    sorry, my last post was very unclear. one train of thought merged into another.

    1. the script writes the result of the sftp co-process to a log, ${datestamp} is unique to every running of the script. therefore if i search for a successful upload string with the unique ${datestamp} I should be able to verify the sftp transfer took place.

    i'm simply looking for any instance of Uploading ${datestamp}.zip to /dir/${datestamp}.zip. (with or without the second datestamp). if there is an instance, then my upload should have been successful. hence using -c so i get a count of how many times the string was found. i do not wish to manipulate the script.log file in any way. only count occurances



    2. while thinking of this, i realized i didn't know what the failed output of an sftp would look like. perhaps a failed sftp transfer will have the string that i'm looking for, followed by "sftp transfer unsuccessful", in which case that string needs to be taken into account. my problem was that i don't know how to simulate a failed sftp to find out what one could look like. i'm sure there's some documentation i can find somewhere about it


    thanks a ton, it looks like grep "Uploading ${datestamp}.zip to .*.zip" zzz is exactly what i was looking for. grep wasn't evaluating the *wildcard correctly, and you explained how to fix it.

    and yes, i likely am doing more work than needed. it's my first shell script that's putting alot together. i scoured the internet for about 4 hours today looking for ways to use the * wildcard and couldn't find a real answer.
     
  8. kronik85

    kronik85 New Member

    Joined:
    Feb 8, 2005
    Messages:
    34,837
    Likes Received:
    0
    Location:
    Deutschland

    worked like a charm :bowdown:
     

Share This Page