1

I have a file (extract below)

1468929555,4,      0.0000, 999999.0000,      0.0000,0,0,0,0,0
1468929555,5,      0.4810,      0.0080,     67.0200,0,4204,0,0,0
1468929555,6,      0.1290,      0.0120,      0.4100,0,16,0,0,0
1468929555,7,      0.0000, 999999.0000,      0.0000,0,0,0,0,0

i want to read in this file and output the results to another file, changing Unix time to human readable - but i only want to do this if field 7 is populated.

#!/bin/bash
file="monitor_a.log"
host=`hostname`
while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
do
mod_time=`date -d @$f1 +"%d/%m/%y %H:%M:%S"`

if [[$f7=="0"]];
then
 done <"$file"
fi

echo "$mod_time,300 ,$host, Apache-a, $f2 , $f5 , $f4 ,  $f3 ,  $f7 , $f8 ,         $f9  ,$f6, $f10" >> mod_monitor_a.log

done <"$file"

The problem lies in my if statement, i get the error

./monitor_convert.sh: line 12: syntax error near unexpected token `done'
./monitor_convert.sh: line 12: ` done <"$file"'

My thinking in the if statement is that if field7=0 the go back to the reading the file into array, the done <"$file" bit. This is obviously incorrect, but i cannot work how to miss this line.

Thanks.

1
  • 2
    Pasting the script in shellcheck.net reveals the problem: if [[$f7=="0"]] is wrong, you need spaces around the brackets. Commented Jul 25, 2016 at 9:54

3 Answers 3

2

Bunch of syntax issues:-

  1. bash if-construct, it should be if [[ $f7 == "0" ]]; and not if [[$f7=="0"]];
  2. Line number 10, done <"$file", the syntax is not allowed. If you are planning to break/continue the loop, just use break/continue constructs.
  3. Do not use legacy command substitution using ``, whereas adopt $(..), refer page for the reason.

Re-formatted script with zero issues/warnings from http://www.shellcheck.net/

#!/bin/bash
file="monitor_a.log"
host=$(hostname)
while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
do
    mod_time=$(date -d @"$f1" +"%d/%m/%y %H:%M:%S")

    if [[ $f7 == "0" ]];
    then
     continue # Process the remaining lines
    fi

     echo "$mod_time,300 ,$host, Apache-a, $f2 , $f5 , $f4 ,  $f3 ,  $f7 , $f8 ,         $f9  ,$f6, $f10" >> mod_monitor_a.log

 done <"$file"
Sign up to request clarification or add additional context in comments.

Comments

1

There are two problems:

The if [[$f7=="0"]] needs spaces, and the "done" inside this if should be a continue:

#!/bin/bash
file="monitor_a.log"
host=`hostname`
while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10
do
  mod_time=`date -d @$f1 +"%d/%m/%y %H:%M:%S"`

  if [[ $f7 == "0.0000" ]]
  then
     continue
  fi

    echo "$mod_time,300 ,$host, Apache-a, $f2 , $f5 , $f4 ,  $f3 ,  $f7 , $f8 ,         $f9  ,$f6, $f10" >> mod_monitor_a.log

done <"$file"

1 Comment

Thanks all. I ended up setting the if statement as a not equal and bringing in the write to file inside the if statement. #######Read in file to array separated by comma while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 do #######Modify Unix time to human readable mod_time=date -d @$f1 +"%d/%m/%y %H:%M:%S" #######Remove all Domains without a hit if [[ $f7 != "0" ]] then printf '%s\n' "$f7" echo "$mod_time,300 ,$host, Apache-a, $f2 , $f5 , $f4 , $f3 , $f7 , $f8 , $f9 ,$f6, $f10" >> mod_monitor_a.log fi done <"$file"
0

This is the code that worked for me in the end, brought the write to new file inside the if statement.

#!/bin/bash
file="monitor_a.log"
host=`hostname`
#######Read in file to array separated by comma  
while IFS=, read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 
do
#######Modify Unix time to human readable
mod_time=`date -d @$f1 +"%d/%m/%y %H:%M:%S"`

#######Remove all Domains without a hit
if [[ $f7 != "0" ]]
then
  printf '%s\n'  "$f7"
  echo "$mod_time,300 ,$host, Apache-a, $f2 , $f5 , $f4 ,  $f3 ,  $f7 , $f8 , $f9  ,$f6, $f10" >> /home/saengers/mod_monitor_a.log
fi

done <"$file"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.