Hi ,
I have a line by line output as follows,for example output of ls
sample1
sample2
sample
i need to check if this output contains the exact string sample.If i use grep , it will find out all strings that contain sample as a part of their word.I dont want to do a pattern matching here but check if one of the line is equal to "sample" (a string comparison).How can i achieve this in shell scripting?
Thanks,
Padmini
Try this,
cat filename | while read input
do
if [[ "$input" = "string to be searched" ]]
then
#do some action if string match
else
# do some action if string does not match
fi
done
# grep --version
grep (GNU grep) 2.5.1-FreeBSD
#grep -w sample file
Hi,
what is "^sample$" ? am not able to understand this.
That's a regex, it tells:
^ #matches the null string at the beginning of a line
sample
$ #matches the null string at the end of a line
literally, search sample, nothing else.
Hi,
But am not getting the correct output if i use ls | grep "^sample$"
Try this,
cat filename | grep -w "sample"
ls -1 | grep -ie "^String$"
Hi,
option 'x' will select only those matches that exactly match the whole line.
Eg:
grep -x "my data" myfile
will select those lines from file myfile which exacly matches 'my data'.
Regards,
Ranjith
And what is the imput and real output , please use [code] tags when you post code or sample data.
EDIT : Too late �� (Why am I missing page n�s... ?_?)
Try grep with -w option:
~$ cat file
a
ab
abc
abcd
abcde
~$ grep abc file
abc
abcd
abcde
~$ grep -w abc file
abc