473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

grep: outputting search strings

1
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.
Mar 9 '08 #1
1 1718
oberon
14
Hi,
one solution is to make a simple shell script. Put the following in a file called e.g. mygrep:
Expand|Select|Wrap|Line Numbers
  1. !/bin/sh
  2. search=$1 #Use first argument as searchword
  3. shift 1 #Remove searchword from argument list
  4. grep $search $* | awk -v search="`echo $search`" {'print search ": " $0'}
You can then run the script with ./mygrep foo *.txt.
Mar 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Zeeshan | last post by:
Hi Everybody, I want to search the filename recursively for "this is the code original" using linux command grep or ls or any other Result should be as follows: FilenamePath and Linenumber in...
1
by: Al Belden | last post by:
Hi all, I've been working on a problem that I thought might be of interest: I'm trying to replace some korn shell scripts that search source code files with perl scripts to gain certain features...
5
by: Hilbert | last post by:
Hello, I've heard of a software on linux that creates a recursive database of text files and then provides an interface for grep-like queries. I'd like to use it to find procedures/variables in...
3
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
4
by: js | last post by:
Just my curiosity. Can python beats perl at speed of grep-like processing? $ wget http://www.gutenberg.org/files/7999/7999-h.zip $ unzip 7999-h.zip $ cd 7999-h $ cat *.htm bigfile $ du -h...
15
by: tereglow | last post by:
Hello all, I come from a shell/perl background and have just to learn python. To start with, I'm trying to obtain system information from a Linux server using the /proc FS. For example, in...
3
by: soniamadtha | last post by:
hello everyone, i have the following values : 98000345 and i would like to search this value in all of the sql files in the directory /temp/*sql normally on unix its grep( ' 98000345 '...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.