Grep problem

i have a log file...in that there is a data like

oss1 F:\app\data\misc\server  012-07-31 08:35:15            8.1.1.
oss2 F:\app\data\misc\server  012-07-31 08:36:15            8.1.2.
oss3 F:\app\data\misc\server  012-07-31 08:37:15            8.1.3.
pss4 F:\app\data\misc\server  012-07-31 08:38:15            8.1.4.
oss5 F:\app\data\misc\server  012-07-31 08:39:15            8.1.5.
oss6 F:\app\data\misc\client  012-07-31 08:40:15            8.1.6.
pss5 F:\app\data\misc\client  012-07-31 08:4115            8.1.7.

i want the output which is having "F:\app\data\misc\server" only

i tried cat logfile.txt|grep "F:\app\data\misc\server"
not working
may i know the reason why its not working????and als need the grep command to filter the results

Try escaping the back slash \

grep 'F:\\app\\data\\misc\\server' logfile.txt

From grep manual:

Care should be taken when using the characters $, *, [, ^, |, (, ), and \ in expression, because they are also meaningful to the shell.

It is safest to enclose the entire expression argument in single quotes ('...').

1 Like

Maybe either single quotes OR escape but not both?

DGPickett, I think it should be single quotes AND escape both.

Single quotes pass \ intact, but double quotes see \ as an escape to put a double quote into the string:

$ echo '\'|cat -vte
\$
$ echo "\"|cat -vte
> ^C
$ 

I use single quotes first and most for their almost complete immunity to unintended meta-behavior. I figure it saves cycles in the shell.

But, hey, the answer is in there somewhere. I flash on FRED buffer scripts where we were seeing 4 or even 8 \ as the number halved every time you went through a meta-wall like the buffer! :smiley:

also in this case its not working


while read LINE
	 do
   echo  $LINE | grep 'F:\\app\\data\\misc\\server'  | grep -v Running 
    done <logfile.txt

But it takes as

while read LINE
	 do
   echo  $LINE | grep 'appdatamiscserver'  | grep -v Running 
    done <logfile.txt

is there any possible way to get as it is??

Look what the shell does to your command (using bash's -vx option):

$ grep 'F:\\app\\data\\misc\\server' file
+ grep 'F:\\app\\data\\misc\\server' file
$ grep "F:\\app\\data\\misc\\server" file
+ grep 'F:\app\data\misc\server' file
$ grep 'F:\app\data\misc\server' file
+ grep 'F:\app\data\misc\server' file

So - follow DGPickett's advice: "Maybe either single quotes OR escape but not both"

Another issue,
In the output file there are 10 lines having the status Running ..i am getting the output as 10 for the below command...

cat output.log |grep 'F:\\app\\data\\misc\\server'| grep Running| wc -l

But when i execute as ksh file it gives wrong output..i am getting RED..
means 0 lines having the status Running(if i give (SERVER_STATUS -eq 0) its showing GREEN)

SERVER_STATUS=`cat output.log |grep 'F:\\app\\data\\misc\\server'| grep Running| wc -l`

if [ $SERVER_STATUS -eq 10 ]
then
     echo "GREEN"
else
     echo "RED"
fi

Replace back-ticks ` ` with $( ) . Also no need to use cat and wc

SERVER_STATUS=$( grep 'F:\\app\\data\\misc\\server' output.log | grep -c Running )

Getting Error..

test.ksh[2]: 0:  not found
test.ksh[4]: test: argument expected
RED

It is difficult to tell what is the issue without seeing the code. Can you set xtrace, run and post the results:

#!/bin/ksh -x
test.ksh[2]: 10:  not found
test.ksh[4]: test: argument expected
RED

you can use sed command right

Try either

SERVER_STATUS=$( grep 'F:\app\data\misc\server' output.log | grep -c Running )

or

SERVER_STATUS=$( grep "F:\\app\\data\\misc\\server" output.log | grep -c Running )

i tried every combination u mentioned but the output was still RED :mad:

I bet you have an extra space in your variable assignment:

SERVER_STATUS= ...

Pls post output.log

hi rudic,
see my post in 1st page

---------- Post updated 01-09-13 at 12:49 AM ---------- Previous update was 01-08-13 at 10:17 PM ----------

anybody please provide a correct solution.

Can you post your code OR set xtrace and verbose, run and post the results?

#!/bin/ksh -xv

I have to second bipinajith - without seeing the trace of the script giving a context to the error msgs you posted, we can't possibly tell the source of the error. It seems to me to be with the test command - here -vx option definitively helps!