Shell Script to look for all lines in a file having '500' in the 11 column taking too long to run

0

I wrote the following script as a practice :

#!/bin/bash

MyFile=$1

while read p; do

error=$(echo $p | awk '{print $11}')

        if [ "$error" = "500" ]
        then
                echo $p
        fi

done < $MyFile

The problem is that the script runs slowly taking hours to complete, my setup :

  • Ubuntu on Windows 10 (64 with intel i7 6400 2.80GHz and 8G Ram).

Is there an issue with my setup or with the logic of the script ? Thanks,

Omar BISTAMI

Posted 2018-01-30T08:40:52.517

Reputation: 267

1I don't really get what you're trying to do or how it's supposed to work but is there any reason grep is insufficient? What size does the input log file have? – Seth – 2018-01-30T08:46:00.390

1Grep is sufficient (grep " 500 " does the trick) i'm just experimenting :) – Omar BISTAMI – 2018-01-30T14:38:07.410

Answers

1

Using $( runs a subshell, so does the pipe |. It's better to do all the work in one shell or one language.

E.g. do all the work in awk:

awk '($11==500){print}' "$1"

Or without shelling out:

while read -a columns ; do
    [[ ${columns[10]} == 500 ]] && echo "${columns[@]}"
done < "$1"

choroba

Posted 2018-01-30T08:40:52.517

Reputation: 14 741