Red_badger
Red_badger

Reputation: 139

Read file in array, if variable is zero do not output line

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.

Upvotes: 1

Views: 63

Answers (3)

Red_badger
Red_badger

Reputation: 139

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"

Upvotes: 0

AwkMan
AwkMan

Reputation: 670

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"

Upvotes: 1

Inian
Inian

Reputation: 85620

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"

Upvotes: 2

Related Questions