String search in UNIX

Hi Team,

Please help me with a command which greps the exact match of the string which I am searching in a file.

For example

cat > file
abc
abcd
def
ghi

In the above file I just wanted to display abc which is first entry.

When I execute grep command cat file | grep "abc" it results both "abc" and "abcd" .

But I wanted to display only the matching text which I am searching. Not the trailing characters.

Please help.

Thanks & regards,
Madhusudan

Hi,

You need to add -w with grep to search for the exact word.

Please check & let know me know.

cat file.txt | grep -w "abc"

Thanks
PKS

2 Likes

Hello maduraju,

Kindly use following command and let me know if that helps.

awk -vA="abc" '($0==A)'  Input_file

Please use code tags for commands/codes/inputs you are using in your posts as per forum rules.

Thanks,
R. Singh

2 Likes

Don't use cat!
If you don't use wildcards like

grep -w ab. file

then use the whole-line-match

fgrep -x abc file