String search in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String search in UNIX
# 1  
Old 05-13-2015
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
Code:
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

Last edited by rbatte1; 05-13-2015 at 09:57 AM.. Reason: Added CODE tags
# 2  
Old 05-13-2015
Hi,

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

Please check & let know me know.
Code:
cat file.txt | grep -w "abc"

Thanks
PKS


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them easier to read and preserves multiple spaces for indentation and fixed-width data.

Last edited by praveenkumar198; 05-13-2015 at 09:56 AM.. Reason: Added CODE tags and other formatting
These 2 Users Gave Thanks to praveenkumar198 For This Post:
# 3  
Old 05-13-2015
Hello maduraju,

Kindly use following command and let me know if that helps.
Code:
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
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 05-13-2015
Don't use cat!
If you don't use wildcards like
Code:
grep -w ab. file

then use the whole-line-match
Code:
fgrep -x abc file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

UNIX command to ignore replacing a search string if it is already present

Hello, I am searching the following string Folder^ in a file and replacing it with Folder^/ However if the file already contains Folder^/ I want to avoid replacing it with Folder^// To do this I have to do the following today: 1) echo "Folder^" | sed 's/Folder\^/Folder\^\//g' I get... (2 Replies)
Discussion started by: mehimadri12
2 Replies

3. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi Don, this is not homework question. I work for a Credit card company and my development goal this year is to learn Unix. I would love if others can help me get started, thanks. Hi everyone I am new to Unix and need help writing a script that can ask user for an input, then search that input... (2 Replies)
Discussion started by: 12ic11
2 Replies

4. Shell Programming and Scripting

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display which volume pool a given... (1 Reply)
Discussion started by: 12ic11
1 Replies

5. UNIX for Dummies Questions & Answers

UNIX Scripting help to input string and search a file to find

Hi everyone, I am new to Unix and need help writing a script that can ask user for an input, then search that input within a file I know will have to use the read and grep commands, anyone can give me somewhere to start would help Task: Write a script to display... (1 Reply)
Discussion started by: 12ic11
1 Replies

6. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

7. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

8. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

9. Shell Programming and Scripting

Search string in unix and print whole matching word

Hi I have requirement to search string starting with specific characters and print whole matching word in that string. example mystr="ATTRIBUTE NAME="Event Name" VALUE="Execute"" I want to search by passing "NAME=" and result should be NAME="Event Name". i am using below command but... (3 Replies)
Discussion started by: tmalik79
3 Replies

10. UNIX for Dummies Questions & Answers

Unix search a string in the text file

File name : Sample.txt Actually i would like to read <schema>Oracle<schema> string from input file and return only once database as my output. Please advise me. Moved to appropriate forum. (1 Reply)
Discussion started by: balajikalai
1 Replies
Login or Register to Ask a Question