grep: outputting search strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep: outputting search strings
# 1  
Old 03-09-2008
grep: outputting search strings

Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string:

An example may help: Suppose I have two files data1.txt and data2.txt:

data1.txt

Hello my name is foo.
What is your name?
Shall we meet at the bar?
Perhaps they will have food.

data2.txt

foo bar foo bar
barbarbarbar
xxxxx
yyyyy
foofoofoo


The command:

grep "foo" *.txt > test.out

returns:

data1.txt:Hello my name is foo.
data1.txt:Perhaps they will have food.
data2.txt:foo bar foo bar
data2.txt:foofoofoo


I would like the command to also include, at the beginning of the line, my original search string (in this case "foo"). Is this possible using grep or perhaps awk, or, something else?

Thanks in advance for your help.
# 2  
Old 03-09-2008
How about this:

x="foo";grep $x *.txt|sed "s/^/$x:/" > test.out

this returns:

foo:data1.txt:Hello my name is foo.
foo:data1.txt:Perhaps they will have food.
foo:data2.txt:foo bar foo bar
foo:data2.txt:foofoofoo
# 3  
Old 03-09-2008
Code:
awk '/foo/{print "foo:"$0}' file > newfile

# 4  
Old 03-10-2008
Hi,

You can put your code in a shell script(a.sh), then use below command which will print the original comman at the first line:

Code:
sh -x a.sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search between two search strings and print the value

Based on the forums i have tried with grep command but i am unable to get the required output. search this value /*------ If that is found then search for temp_vul and print and also search until /*------- and print new_vul Input file contains: ... (5 Replies)
Discussion started by: onesuri
5 Replies

3. Shell Programming and Scripting

Whether we can search multiple strings using or in grep -F

Hi, Whether we can search multiple strings using or in grep -F In Generally, grep -F "string1" "filename.txt" How to search for multiple string using grep -F as we using grep grep "string1\|string2" "filename.txt" Regards, Nanthagopal A (10 Replies)
Discussion started by: nanthagopal
10 Replies

4. Shell Programming and Scripting

Outputting discarded GREP lines to a file

I had a question about grep. If I grep for something in a file, the output shows me all the lines in which that 'something' is contained in. Is there a way to also output all the lines in which that 'something' wasnt contained in. Say I have a file with a bunch of names and I do: grep scott... (2 Replies)
Discussion started by: ndedhia1
2 Replies

5. Shell Programming and Scripting

Search between 2 strings

Guys any pointers on how to search between 2 sets date strings with time in the below file example :- 02-Feb-2010 23:12:09 GMT event_type::event_details_are_like_this 02-Feb-2010 09:10:29 GMT event_type::event_details_are_like_this 03-Feb-2010 11:12:19 GMT... (3 Replies)
Discussion started by: lavascript
3 Replies

6. Shell Programming and Scripting

outputting data in table from grep results

Hi all. I'm a real unix newbie and looking for some help on a shell scripting problem. I'm going the longest ways around everything but I'm getting there. My next problem however has me stumped. I have set up a program that takes inputs from a user for a particular month and year (and stores them... (2 Replies)
Discussion started by: Chillspark
2 Replies

7. Shell Programming and Scripting

how to search with 2 strings.

Hi, i have a file a.txt like -------------------------------- col1|col2|col3 data1|data2|data3 other1|other2|other3 -------------------------------- i need to search 2 strings(data in a.txt file is case sesnsitive), suppose data1 and data2. If these 2 strings found then only i need... (2 Replies)
Discussion started by: syamkp
2 Replies

8. Shell Programming and Scripting

Search between strings with an OR

Hi have Input in this way KEY AAAA BBBB END1 KEY AAAA BBBB END2 KEY AAAA BBBB END3 I need to find any thing matching in between KEY And ending with "END1|END2|END3" This didnot work awk '/KEY/,/END1|END2|END3/' (3 Replies)
Discussion started by: pbsrinivas
3 Replies

9. UNIX for Dummies Questions & Answers

how to use GREP comaand to search strings in This OR That scenrio

Hi , Can anyone suggest how to use the Grep command to find multiple strings like kind of situation find 'black' or 'white' in file colors.txt I tried : $ grep "black\|white" colors.txt but no results. thanks, VJ (4 Replies)
Discussion started by: vj_76
4 Replies

10. UNIX for Dummies Questions & Answers

Search for strings

I am trying to replace the word ACTIVE with 2002 in a file and I am getting the following error. Does anyone know what this means? $ sed "s/ACTIVE/2002" mydata.txt > yourdata.txt sed: 0602-404 Function s/ACTIVE/2002 cannot be parsed. (5 Replies)
Discussion started by: lesstjm
5 Replies
Login or Register to Ask a Question