I've a sequence of commands to be used along with lot of pipings, something like this:
awk '{ if ($8 ~ "isad") {print $2, $5, "SE"} else {print $2, $5, "ANT"} }' "/var/log/apache2/other_vhosts_access.log" | grep -v '127.0.0.1' | tr '[' '\0' | tr [:lower:] [:upper:] | sort -t' ' -s -k3
This basically filters the Apache log and prints three cols of info. First col is IP address, second is time, third is a string. The output could be sorted based on any column so, I need to use '-m' with sort for the time field. Sorting order could also be reversed.
I want a string to store the arguments to sort, and let the combined strings get executed. Something like this:
$AWK_CMD | $SORT_CMD $SORT_KEY $SORT_REV
where
SORT_CMD="sort -t' '"
SORT_KEY="-k$1" # $1 from cmd line arg
SORT_REV="$2" # Either -r or blank
Am able to build such strings; when I echo it, it looks fine. Problem is, how to execute it? I get some errors like:
awk '{ if ($8 ~ "isad") {print $2, $5, "SE"} else {print $2, $5, "ANT"} }' "/var/log/apache2/other_vhosts_access.log" | grep -v '127.0.0.1' | tr '[' '\0' | tr [:lower:] [:upper:] | sort -t' ' -s -k3
Running ...
awk: '{
awk: ^ invalid char ''' in expression
Please ignore if 'sort' doesn't sort properly, that can be fixed. I wish to know how I can build the final command string in steps. The ways I tried to execute this command in the script is by:
$final_cmd
`$final_cmd`
Edit: The script that I'm trying to use
KEY=$1 # Values {1, 2, 3}
REVERSE=$2 # Values {0, 1}
SORT_CMD="sort -t' ' -s"
SORT_KEY="-k$KEY"
if [[ $KEY -eq 2 ]]
then
SORT_KEY="-m -k2"
fi
if [[ $REVERSE -eq 1 ]]
then
SORT_REV="-r"
else
SORT_REV=
fi
final_cmd="awk '{ if (\$8 ~ \"isad\") {print \$2, \$5, \"SE\"} else {print \$2, \$5, \"ANT\"} }' $LOG_FILE '|' grep -v '127.0.0.1' '|' tr '[' '\0' '|' tr [:lower:] [:upper:] '|' $SORT_CMD $SORT_KEY $SORT_REV"
echo $final_cmd
echo "Running ..."
$final_cmd