8

I have the following script

#!/bin/bash
set i=0;
while read line
do
  echo "$line";
  $i < cat "my.log" | grep -w '$line' | wc -l;
  echo "$i";
  i=0;
done < "test.txt"

test.txt has values like

abc
def
lmn

my log has values like

> INFO 2013-08-16 13:46:48,660 Index=abc insertTotal=11  
> INFO 2013-08-16 13:46:48,660 Index=abcd insertTotal=11  
> INFO 2013-08-16 13:46:48,660 Index=def insertTotal=11  
> INFO 2013-08-16 13:46:48,660 Index=abcfe insertTotal=11

The goal is to pick each value from the test.txt and to search for that pattern in the my.log file.

The number of times the pattern is found is assigned to the variable i.

I am going to check the value of i and if it is 0 or the pattern was not found then I want that pattern to be stored in another file.

Currently, I am getting

./parser.sh: line 7: cat: No such file or directory

I have tried the cat command on the command line and it has worked fine but it is giving this error from within the script.

Macky
  • 433
  • 2
  • 9
  • 22

2 Answers2

7

You are trying to use a file called cat as input to a command called 0. That won't work.

Instead of

$i < cat "my.log" | grep -w "$line" | wc -l;

do this:

i=$(cat "my.log" | grep -w "$line" | wc -l)

See the question Difference between single and double quotes in Bash for a discussion of quotes and http://tldp.org/LDP/abs/html/commandsub.html for a description of command substitution in bash.

Community
  • 1
  • 1
  • Thanks @Tichodroma. That worked although I didn't get the expected result. The value of my variable was still 0 even though I know that the file has 1 occurence of the pattern. – Macky Aug 19 '13 at 09:59
  • Why do you use the `-w` option of grep? It means 'Select only those lines containing matches that form whole words.' Is this what you want? –  Aug 19 '13 at 10:00
  • Yes, so basically I have values like abcd abc abcde on different lines. I want to pick the lines where the search pattern (for example : abc) matches the value exactly. – Macky Aug 19 '13 at 10:02
  • Please edit your question to include a real sample of both files. –  Aug 19 '13 at 10:03
  • Done. please take a look. Thanks. – Macky Aug 19 '13 at 10:10
  • Brilliant. It works. Could you please explain the solution or point me to a link. – Macky Aug 19 '13 at 10:23
  • Thank you @Tichodroma. If i could give you bonus points,I would :-) – Macky Aug 19 '13 at 10:47
  • Using a docker container, this doesn't work for me. I copy-pasted the following line i=$(cat "my.log" | grep -w "$line" | wc -l) and got cat: my.log: No such file or directory – Banoona Jun 26 '18 at 06:33
1

I had error messages like:

cat: command not found
curl: command not found

It was caused by code like this:

export PATH=$1
export SERVER=$(cat s_remote)

curl $@ $SERVER$PATH

And the solution was to rename the PATH variable to something else. PATH is used locally by the shell, and setting it for a different purpose interferes with the shell.

gregn3
  • 1,728
  • 2
  • 19
  • 27