0

in my awk output (placed in php) i need to convert epoch time to normal, what's wrong with scriptm help plz. Apache error log: sh: 1: Syntax error: Unterminated quoted string

$result = shell_exec("grep -i 'SERVICE ALERT' /home/den/-".$date."-00.log | sed 's/ SERVICE ALERT:/;/g; s/^.//g;s/]//g' | awk -F ';' '/WARNING|CRITICAL/ {print 'date -d@'$1',$2,$3,$4}' | sort | uniq  -c | sort -nr | head -10");
dend
  • 13
  • 1
  • 7
  • 4
    `awk -F ';' '/WARNING|CRITICAL/ {print 'date -d@'$1',$2,$3,$4}'` is plainly wrong. You cannot execute an external command like that. Check [converting dates in awk](http://stackoverflow.com/q/2121896/1983854). – fedorqui Oct 08 '14 at 10:49

1 Answers1

1

GNU awk has some built-in time functions:

gawk '{print strftime("%F %T", $1), $2, $3, $4}' <<END
1234567890 foo bar baz
END
2009-02-13 18:31:30 foo bar baz

You'll have to escape those double quotes somehow in your gigantic double-quoted shell command. and since you're sorting based on the date-time, use a format like "%Y%m%d-%H%M%S"


Since awk can do what grep and sed do, I'd fold those commands into awk to reduce the pipeline a bit. You can decide if it improves or reduces maintainability.

I'm not familiar enough with PHP to know if it can handle the extra whitespace shown here:

$result = shell_exec(
            "gawk -F ';' '
                ! /SERVICE ALERT/ {next} 
                {gsub(/ SERVICE ALERT:/, ";"); sub(/^./, ""); gsub(/\]/,""); $1=$1}
                /WARNING|CRITICAL/ {print strftime("%F %T", $1),$2,$3,$4}
            ' /home/den/-".$date."-00.log | sort | uniq  -c | sort -nr | head -10"
);
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • $result = shell_exec("grep -i 'SERVICE ALERT' /home/den/-".$date."-00.log | sed 's/ SERVICE ALERT:/;/g; s/^.//g;s/]//g' | awk -F ';' '/WARNING|CRITICAL/ '/WARNING|CRITICAL/ {print $1, $2, $3, $4}' | sed 's/^.*\([0-9]\{10\}\).*/date -d@\\1/' | sh | sort | uniq -c | sort -nr | head -10"); I made sed with date -d, but script output only the column with date, and i need also columns $2 $3 $4 in output, maybe you can help me? – dend Oct 09 '14 at 06:14
  • I did help you. Did you try it? – glenn jackman Oct 09 '14 at 10:52
  • I dont understand where should i place this gawk – dend Oct 10 '14 at 11:52