May 25, 2012
tom

Issues with echo commands?

Question

I run this script as quanta suggest me

#!/bin/bashEDATE=$(tail -1 a | awk '{ print substr($4, 2, length($4)) }')
EEPOCH=$(date -d "$(echo "$EDATE" | awk 'BEGIN { FS = "[/:]"; } \
           { print $1" "$2" "$3" "$4":"$5":"$6 }')" +%s)
time=$(expr 60 \* 60 \* 24 \* 5)
SEPOCH=$(expr $EEPOCH - $time)while read line
do
    DATE=$(echo $line | awk '{ print substr($4, 2, length($4)-1) }' | \
        awk 'BEGIN { FS = "[/:]"; } { print $1" "$2" "$3" "$4":"$5":"$6 }')
    DEPOCH=$(date -d "$DATE" +%s)
    [[ $DEPOCH -ge $SEPOCH && $DEPOCH -le $EEPOCH ]] && echo $line | \
        awk '{ print substr($4, 2, length($4)) }' >> as1
done < a

I checked that and it seems that your script want to check log file line by line. Since it has more than 14000 items it takes a lot. So when I run it the cursor goes to next line and next line and I should press it 14000 times so that the whole log file be checked! It’s impossible! It just work for short log file I think. Is the problem because of while?

Asked by matarsak

Answer

This one liner (I’ve split it for clarity) should give you the same result. You can optionally add > as1 to the end to redirect the output to a file. Put the path to the apache log file where I’ve put <logfile>

for d in \
 $(sed -nre 's/.*\[(..)\/(...)\/(....):(..:..:..) .*/\1 \2 \3 \4/p' <logfile> | date +%s -f-);
do echo $[ $d - 86400 * 5]; done

The date command doesn’t need an explicity UTC formatted date for the -dargument, although it doesn’t understand dates as the apache logs output them, so I’ve done some substitution to swap the slashes and the colon separating the date and time with spaces.

Answered by SmallClanger

Related posts:

  1. find a date in a log file
  2. How to ‘echo “” > x’ on multiple files
  3. Unexpected Awk results with sh -c
  4. unix command extract field from text file
  5. Bash sequential commands. Calling functions without launching new threads

Leave a comment