Result of the grep is not storred correctly into the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Result of the grep is not storred correctly into the variable
# 1  
Old 09-07-2010
Result of the grep is not storred correctly into the variable

I am facing a problem while storring the grep results into a variable. I need to count the occurence of the pattern \, in a file and store that in a variable. I have given the below command

p=`grep -c '\\,' filename`

But while echoing the variable, i am getting the total number of lines in the file instead of the count of lines having pattern \, . If i give grep directly i am getting the correct result. Please help Smilie

$grep -c '\\,' filename
1
$p=`grep -c '\\,' filename`
$echo $p
29

Regards
Renjith
# 2  
Old 09-07-2010
Quote:
Originally Posted by renjithv
... i am getting the total number of lines in the file instead of the count of lines having pattern \, . ...
Code:
$ 
$ 
$ cat f2
Nothing on this line
this line has \, and \, and \, here
and finally \, and \,
but nothing here
$ 
$ 
$ grep '\\\,' f2
this line has \, and \, and \, here
and finally \, and \,
$ 
$ 
$ grep -c '\\\,' f2
2
$ 
$ x=$(grep -c '\\\,' f2)
$ 
$ echo $x
2
$ 
$ 

(A) Number of lines in file f2 that have the pattern "\," = 2.
(B) Number of occurrences of the pattern "\," in those 2 lines = 5.

I've assumed you want (A), although the following two statements in your post are contradictory:

Quote:
... I need to count the occurence of the pattern \, in a file and ...
and

Quote:
... i am getting the total number of lines in the file instead of the count of lines having pattern \, . ...
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 09-07-2010
Thanks tyler_durden!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to store result of grep into a variable?

In a directory i have a file *output* whose contents are changing for every simulation. zgrep "trace file is" *output* | zgrep g o/p: trace file is Int_01.1352176388z4f56ec33.0.trace.gz I want to extract "Int_01.1352176388z4f56ec33.0.trace.gz" from the above result into a variable. i... (2 Replies)
Discussion started by: twistedpair
2 Replies

2. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Grep Regexp not working correctly

Consider the following code: grep -o -e '^STEAM_::\d+$' workfile3.tmp A sample format of a valid string for the regexp would be: STEAM_0:1:12345678 Here is an example line from the workfile3.tmp file: 465:L 01/02/2012 - 00:05:33: "Spartan1-1-7<8><STEAM_0:1:47539638><>" connected No... (2 Replies)
Discussion started by: spinner0205
2 Replies

4. UNIX for Dummies Questions & Answers

How do I grep a Date correctly

I am still a novice at this stuff, but I have searched everywhere and I cant seem to get this working. I am using a database program that I need to pull information from. The command I am using is the following. search /project | grep "date -v -1m "+%Y-%m"" This returns no results, however... (1 Reply)
Discussion started by: trezero
1 Replies

5. Shell Programming and Scripting

Grep/awk not getting the message correctly

I have a script which will take two file as the inputs and take the Value in file1 and search in file2 and give the output in Outputfile. #!/bin/sh #. ${HOME}/crossworlds/bin/CWSharedEnv.sh FILE1=$1 FILE2=$2 for Var in $(cat $FILE1);do echo $Var grep -i "$Var" $FILE2 done > Outputfile I... (2 Replies)
Discussion started by: SwapnaNaidu
2 Replies

6. UNIX for Dummies Questions & Answers

grep -A switch not working correctly with -m

egrep -A 7 -m 2 -h 'Date:|Time:' *.html this is showing only 2 line after the context of the 2nd found match. Is this a bug in grep? egrep -A 7 -m 2 -h 'Time:' *.html - this works correctly (2 Replies)
Discussion started by: zer0
2 Replies

7. Shell Programming and Scripting

Is grep being used correctly?

My goal is to find files contain the "signal 11" string in a specific directory. I need the file details followed by the string. I wrote a script to test out and play with Shell command since it's my first time to write a Shell script. Let me cut the story short... this command line: if ... (2 Replies)
Discussion started by: sai0899
2 Replies

8. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

9. Shell Programming and Scripting

Variable not working correctly.

Hi, I have a script where I am trying to set a local variable using the following, MYVAR="$NAME"_"$NAME2".txt where say, NAME = one NAME2 = two so I want the output one_two.txt but what I am getting is, two.txt basically the $NAME2 is overwriting, what am I doing wrong? ... (3 Replies)
Discussion started by: walsh_j
3 Replies

10. Shell Programming and Scripting

Variable with $ do not show correctly

Hey guys i need help i have a script to push a password to a remote server the only problem is the $ENCRYPT variable has $'s in it (from the encrypted password in the shadow file) and they drop out when apending to the shadow file via the usermod command so $1$Q/6a08n$EoAcBuR/YnoCQC shows up as... (3 Replies)
Discussion started by: insania
3 Replies
Login or Register to Ask a Question