Display only found string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display only found string
# 1  
Old 01-11-2007
Display only found string

Is there a way for grep to output only the found string and not the whole line?

I have a ksh script which reads in a file and loops through every line looking up on a grep -f list. For it to only display only the string found i pass this to awk as a variable and loop through the list file using match and print the list line with the string found.

Can this be done any simpler using ksh?

Thanks

Ste
# 2  
Old 01-11-2007
See if you have the -o flag for grep. If yes use
Code:
grep -o "needle" haystack

If no, try sed.
Code:
sed -n -e "s/.*\(needle\).*/\1/p" haystack

# 3  
Old 01-11-2007
Code:
grep -f list file | sed "s/ /\\
/g" > tmp
grep -f tmp list

# 4  
Old 01-11-2007
if there is no -o option available with grep

Code:
grep needle haystack 2>/dev/null
if [ $? -eq 0 ]
then
  echo needle
fi

# 5  
Old 01-11-2007
thanks for the quick replies,

we don't have the "grep -o" option, the answer which works best for my needs is:

sed -n -e "s/.*\(needle\).*/\1/p" haystack

Again thanks for all the replies,

Ste
# 6  
Old 01-21-2007
Since you only want to know if the string is there:

grep "string" file > /dev/null && echo "string"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a tag and display a message if not found.

Hi All, I am working with a XML file. Below is part for the file. <Emp:Profile> <Emp:Description>Admin</Emp:Description> <Emp:Id>12347</Emp:Id> </Emp:Profile> <Emp:Profile> ... (7 Replies)
Discussion started by: Girish19
7 Replies

2. UNIX for Dummies Questions & Answers

Display lines after match is found within specified range

I have a file which has collection of segments occuring n(For eg.100) times ISA GS ST NM1*85 N3 N4 NM1*IL N3 N4 REF*D9*1001 ISE GE SE ISA GS ST NM1*85 N3 (13 Replies)
Discussion started by: nsuresh316
13 Replies

3. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

4. UNIX for Dummies Questions & Answers

Display n lines before match found

I have a file with following data A B C D E F G H I K L M N and search pattern is G Expected output (3 Replies)
Discussion started by: nsuresh316
3 Replies

5. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

6. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

7. Shell Programming and Scripting

How to grep for message and if found display filename?

Hi i'm new to the forum and was hoping someone could help me with the following query. I do alot of testing and have hundreds of log files output. I have a script (someone else wrote) which finds all the passed and failed logs and puts a number in a column onto a webpage: e.g: Pass ... (4 Replies)
Discussion started by: defamer
4 Replies

8. UNIX for Dummies Questions & Answers

Display few rows before and after the pattern is found

hi, i'm trying to write a script in ksh that checks the build log for errors. I use egrep to serch for a pattern list. When the pattern is found i want to print 2 rows before and 2 rows after the line where the patern is found, but i don't know how to do it. (3 Replies)
Discussion started by: lavinia_f
3 Replies

9. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

10. Shell Programming and Scripting

Search for string and display those NOT found

In my script I read a input file and search all the files in a directory and it's sub-directories for that string using: find . -type f -print | xargs grep $var1 This just displays all the lines the string was found on. Too much data. What I need is to store in a file one time those... (17 Replies)
Discussion started by: John Rihn
17 Replies
Login or Register to Ask a Question