list length

Hello,

How can I change this code

a=$(egrep "exp" $logFiles -oh);
aCount=$(egrep "exp" $logFiles -oh| wc -l);

to something like this

a=$(egrep "exp" $logFiles -oh);
aCount = $a| wc -l

What I mean I do not want to execute the egrep twice because it takes a lot of time

Regards

aCount=$(echo "$a" | wc -l)

Thanks, It worked. But for some reasons I get 1 even though no match was found. Do you know the reason.

---------- Post updated at 12:05 PM ---------- Previous update was at 11:56 AM ----------

Thanks alot, It worked. But for some reasons I get 1 even though no match was found.

a=$(egrep  "exp" $logFiles -oh);
echo $aCount;
aCount = $(echo"$a"| wc -l);
echo  $aCount;
....
....

This code is executed in side a loop and I get this result
0
1
0
1
Note that a is empty

did you enclose the $a in between double quote ?

# a=$(grep Data tst)
# echo $a
<td>Good Data 1 </td> <td>Good Data 2</td> <td>Good Data 3</td> <td>Good Data 1 </td> <td>Good Data 2</td> <td>Good Data 3</td>
# echo "$a"
  <td>Good Data 1 </td>
  <td>Good Data 2</td>
  <td>Good Data 3</td>
  <td>Good Data 1 </td>
  <td>Good Data 2</td>
  <td>Good Data 3</td>

Oh, and remove the <space> before and after the = sign and add a space after the echo
should be

aCount=$(echo "$a" | wc -l)

... and NOT:

aCount = $(echo"$a" | wc -l)

depending on your OS, check if there are no option needed with the echo command like echo -e or echo -n or whatever.
by the way, to troubleshoot your code, you can just add the following line

echo "a=$a"

just before the

aCount=$(echo "$a" | wc -l)

So you can check what do $a really contains and see if the 1 0 1 0 1 ... is correct... or not

The problem is that "wc" counts the number of line-feed characters. The echo "$a" line is generating a line-feed even if $a is empty.

Try avoiding the intermediate variable:

aCount=$(egrep -oh "exp" $logfiles|wc -l)

Also note the normal position of the options parameters on the "egrep" command line. Though your version happens to work for "egrep" in Linux, wandering from the command line described in a "man" page will eventually give you grief.

Also, please stop putting semi-colons at the end of lines. This is unix scripting not Oracle.

LOL :D:D:D

Regarding the wc stuff, then

[[ -z "$a" ]] && aCount=0 || aCount=$(echo "$a" | wc -l)

Thanks Guys for help, and I like SQL