7

I'm trying to find the number of times a string is repeated in a file, at the same time i've to store it in a variable.

When i use the command (cat filename | grep -c '123456789') , it displays the count correctly but when i use the below command it shows as command not found.

var =$(cat filename | grep -c '123456789')
echo $var

Can u let me know where i'm wrong ?

2
  • Do you need the single quotes around your search string for grep?
    – Levon
    Jun 5, 2012 at 5:27
  • @Levon, single quotes around the regex for grep would only be required if the regex contains characters that would be interpreted by the shell, like $ or [ or ]. As a very simple regex containing no special characters, 123456789 doesn't need to be quoted.
    – ghoti
    Nov 27, 2015 at 12:20

3 Answers 3

16

Don't use spaces around the = sign:

var=$(cat filename | grep -c '123456789')

Read at least the Bash Prog Intro Howto

But you've got a useless use of cat so code simply

    var=$(grep -c '123456789' filename)
0
6

Remember that grep can read the file directly. You can avoid useless use of cat.

In the example in your question, the command not found error occurs because of the space before the equals sign. Try this instead:

var=$(grep -c '123456789' filename)

or if you're using bash:

read var < <(grep -c '123456789' filename)

or (for completeness) in csh/tcsh:

setenv var `grep -c '123456789' filename`
2
  • This works fine since because i'm directly inputting the data. declare -a arr arr=( $(awk '/123456789/{print NR}' filename) ) echo ${arr[0]} echo ${arr[]} Bt when i do as below, it doesn't work. Can u let me know how the parameter 'name' can be used int command below : echo Enter your search string: read name declare -a arr arr=( $(awk '/"$name"/{print NR}' filename ) echo ${arr[0]} echo ${arr[]} Jun 5, 2012 at 5:53
  • 2
    It's difficult to understand what you're asking based on this comment, possibly due to the mess made by SO's formatting. If you need to add more detail to your question, please click the edit link under the question, make your changes, and I'll update my answer accordingly.
    – ghoti
    Jun 5, 2012 at 5:57
1

Using backquotes will also work:

varx=`( cat filename| grep -c '123456789' )`

I.e., the $ is not required, you can assign the output of various commands to variables through the use of backquotes.

For instance:

$ pwd
/home/user99

$ cur_dir=`pwd`
$ echo $cur_dir
/home/user99
7
  • 1
    That works because you removed the space before the = ; you could have kept the $(...) notation (more readable, and nestable) instead of the backquotes Jun 5, 2012 at 5:04
  • @ Levon : Isn't it necessary to use $ symbol in above command ? Jun 5, 2012 at 5:13
  • @JackieJames Not necessary, the command works as shown, you can use the backquotes instead. I've used this both with bash and the tcsh. Did you run into problems trying this?
    – Levon
    Jun 5, 2012 at 5:15
  • @ Levon : varx=( cat filename| grep -c 123456789' )` echo $varx It doesn't print anything. Jun 5, 2012 at 5:17
  • @JackieJames Can you copy directly from my answer (I tested this, using a different file and grep string of course). What you just posted seem to miss the leading backquote, the single quote in front of the 1234, and it looks as if you have a space after the = ... Let me know
    – Levon
    Jun 5, 2012 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.