Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I have a homework question that I have been working on since yesterday, and I'm not making any progress. I'm not asking for the answer, just a push in ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    7

    Smile grep question

    Hello, I have a homework question that I have been working on since yesterday, and I'm not making any progress. I'm not asking for the answer, just a push in the right direction so I can try to do it myself. Here's the problem:

    I have a bunch of directories and sub-directories that all contain emails. Each email is an individual file where the first 6 lines are:

    ThreadNum:
    Subject:
    To:
    From:
    TimeStamp:
    Sent:

    I want to create a bash script that generates a summary file. There would be one row for each email, and each row would have the 6 fields above seperated by a comma or something.

    Again, please don't post an answer. I'm not looking to cheat, I am just out of ideas. Thanks!

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Maybe grep's -A option would be useful.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, if you're looking for the first six lines of a file, the 'head' program might be useful. You could use the command 'head -n6' to get the first lines.

    Combining this with a loop might be helpful.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Jan 2009
    Posts
    7
    Thanks for your help! Here's what I came up with. I know it's probably the least efficient way on earth to do it, but it works...

    Code:
    #!/bin/bash
    
    ARCHIVE=/home/brad/CS571/week3/TEST_DrMathArchive
    
    # Prepare the BradsTempFile file in the current directory
       echo "" > ./BradsTempFile
    
    # FOR EACH FILE IN THE $ARCHIVE DIRECTORY,
    for FILE in `ls $ARCHIVE`; do
    
    # TEST IF THE FILE IS A DIRECTORY
       if [ -d $ARCHIVE/$FILE ]; then
    
       # ACTION IF $FILE IS A DIRECTORY
    
          # LOOP THROUGH THE FILES ONE AT A TIME
             for EmailFile in `ls $ARCHIVE/$FILE`; do
    
             # Find the first instance of "From:"
                strFrom=`awk '/^From: / {print $2,$3,$4,$5,$6}' $ARCHIVE/$FILE/$EmailFile`  
            
             # Find the first instance of "TimeStamp:"
                strDate=`awk '/^TimeStamp: / {print $2}' $ARCHIVE/$FILE/$EmailFile`
    
             # Split date out into Month and Year
                strDateM=${strDate:0:2} 
                strDateY=${strDate:6:4}
                
             # Create the data line by joining fields together
                DataLine=`echo $strFrom "-" $strDateM $strDateY`
    
             # Send the data line to the temp file
                echo $DataLine >> ./BradsTempFile
    
             done
        fi
    done

Posting Permissions

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