egrep output each file in new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting egrep output each file in new line
# 1  
Old 02-25-2010
egrep output each file in new line

I have the following script that searches in several files and shows the search results and the matches filename on the screen.

Code:
find . -exec egrep -wH "word1|word2" {} \;

the output from the search display as:
Code:
file1
word1
word2

I need to show each file search output result on new line.
for instance:
Code:
file1:word1:word2

some of the files missing both string of the search query or having blank spaces between words,

Anyone know how I can get each file search results on new line?

Thanks

Last edited by Scott; 02-25-2010 at 07:51 PM.. Reason: Please use code tags
# 2  
Old 02-25-2010
Quote:
Originally Posted by konddor
...
Code:
find . -exec egrep -wH "word1|word2" {} \;

the output from the search display as:
Code:
file1
word1
word2

...
It doesn't show up like that on my system (Linux) -

Code:
$ 
$ find . -exec egrep -wH "word1|word2" {} \;
./file2:word2
./file3:word2
./file3:word1
./file1:word1
./file1:word2
$ 
$

tyler_durden

How do you want this output re-arranged ?
# 3  
Old 02-25-2010
Thanks durden

i was running the search command:
# find . -exec egrep -iH "word1|word2" {} \; (with "i" instead of "w")

with the files content as:
word1
novilf
polbem
word2
lofteer

and i got the following output:

./file1:word1
./file1:word2
./file2:word1
./file2:word2
./file3:word1
./file3:word2

instead im trying the output to be as:
./file1:word1:word2
./file2:word1:word2
./file3:word1:word2


appreciate for your help
# 4  
Old 02-25-2010
Code:
 find . -exec egrep -iH "word1|word2" {} \;  |awk -F ":" '{a[$1]=a[$1]":"$2 } END {for (i in a) print i""a[i]}'

# 5  
Old 02-25-2010
Thanks rdcwayx

yeeeah, i used it and it's works like a charm

can you explain the awk commands, im a newbie and trying to understand on scripting

Thank you!
# 6  
Old 02-26-2010
Code:
$ 
$ find . -exec egrep -iH "word1|word2" {} \;
./file2:word2
./file3:word2
./file3:word1
./file1:word1
./file1:word2
$ 
$ 
$ find . -exec egrep -iH "word1|word2" {} \; | perl -ne '{ chomp; @x=split/:/;
             if ($x[1] eq "") { print; $p=$x[0] }
             elsif ($x[0] eq $p) { print ":",$x[1] }
             else { print "\n",$_; $p=$x[0] }
           } END {print "\n"}'

./file2:word2
./file3:word2:word1
./file1:word1:word2
$ 
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. UNIX for Dummies Questions & Answers

Egrep output

If I have an output from egrep that has two elements on separate lines can I concatenate them? so the egrep for examople might be: egrep "filename|type" And the actual output might be: ./file001.doc Type Text Document How do I get the egrep to remove the line feed so the output is: ... (5 Replies)
Discussion started by: TuftyDave
5 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Using egrep to output expressions which are not found

Hi, Im using the below command to search a file for multiple expressions if the 4th expression doesnt exist the command simply lists what it can find and ignores what it cant. Is there any way to get the command to output an error or a message if it cant find the 4th expression to a file? ... (16 Replies)
Discussion started by: 02JayJay02
16 Replies

5. Shell Programming and Scripting

egrep output order

The order of egrep output seems to be as they occur in the file. How do I get the order as requested? For e.g. file contents: AAA EEE GGG egrep 'GGG|AAA|EEE' file gives AAA EEE GGG instead of GGG AAA EEE (2 Replies)
Discussion started by: madhavb
2 Replies

6. UNIX for Dummies Questions & Answers

egrep line which begging with

HI Guys, I want egrep from ./txt file which begging from Hello. File A.txt Hello A Abc Abc Xyz Hello B Hello C .. ... Output File B Hello A Hello B (2 Replies)
Discussion started by: asavaliya
2 Replies

7. Shell Programming and Scripting

How to grep/awk/egrep two values for given output?

Dear Friends, I have a command which can result following output. Packet is: /var/adm/yyyy/pkt6043 Intended for network : /vob/repo I would like to retrive pkt6043 and /vob/repo using single command. Blue color test will be always contstant and red color text will be dynamic ... (2 Replies)
Discussion started by: baluchen
2 Replies

8. Shell Programming and Scripting

egrep problem | printing next line?????

Hi !!! Dear People, Please help me with the following problem. consider this output: Top 5 Timed Events ~~~~~~~~~~~~ Total Event Waits Time (s) Ela Time ---------------------------- ------------ ----------- ----- CPU time ... (3 Replies)
Discussion started by: user__user3110
3 Replies

9. Shell Programming and Scripting

Comma Delimited Output w/ egrep

Hi all, I have an input file that I am pulling out certain phases using the following commands: cat /nodes.txt | egrep -e 'OSVersion|PrimaryNodeName' Currently the output looks like this: OSVersion - 5.0 PrimaryNodeName - serverA OSVersion - 5.0 PrimaryNodeName - serverB OSVersion... (2 Replies)
Discussion started by: indianadoug
2 Replies

10. UNIX for Dummies Questions & Answers

adding blank line in egrep

I followed the egrep example given in the thread "parse text or complex grep ?". It is exactly what I need...except... how do I insert a blank line after the second line? My exact command is: egrep 'patt1|patt2' filename the result is: patt1 patt2 patt1 patt2 and so on. I would... (2 Replies)
Discussion started by: antalexi
2 Replies
Login or Register to Ask a Question