konddor
1
I have the following script that searches in several files and shows the search results and the matches filename on the screen.
find . -exec egrep -wH "word1|word2" {} \;
the output from the search display as:
file1
word1
word2
I need to show each file search output result on new line.
for instance:
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
It doesn't show up like that on my system (Linux) -
$
$ 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 ?
konddor
3
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
rdcwayx
4
find . -exec egrep -iH "word1|word2" {} \; |awk -F ":" '{a[$1]=a[$1]":"$2 } END {for (i in a) print i""a}'
konddor
5
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!
$
$ 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