Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find multiple matching in a file

Status
Not open for further replies.
Jul 17, 2003
155
US
Hi,

I have files that have many of the following data in the file:


Start: 9:20
End: 10:30
Clone: 11:50
test test test
....
Start: 11:55
End: 12:54
...
test test
Start: 01:22
End: 04:45
Clone: 06:20

---
I want to extract all lines that contain "Start Stop and Clone"

I tried using find, but couldn't get it to work.

Thank you for any help,
Chris
 
man [tt]grep[/tt].

[tt]find[/tt] is for finding files, not lines within files.
 
How can I use "grep" to find a pattern and also display the line number corresponding to the matched pattern. For example, the output could look like this:

1 Start
2 Stop
3 Clone
6 Start
7 Stop
12 Start
13 Stop
14 Clone
 
[tt]man grep[/tt].

Pay attention to the [tt]-n[/tt] and [tt]-o[/tt] options.
 

Below a script will return:-
============================
Start: 9:20
Start: 11:55
Start: 01:22
Start: 01:25
Start: 01:32

End: 10:30
End: 12:54
End: 04:45
End: 04:55
End: 04:41

Clone: 11:50
Clone: 06:20
Clone: 06:40
Clone: 06:50

from the file abc:-
===================
Start: 9:20
End: 10:30
Clone: 11:50
test test test
....
Start: 11:55
End: 12:54
...
test test
Start: 01:22
End: 04:45
Clone: 06:20
...
test test
Start: 01:25
End: 04:55
Clone: 06:40
...
test test
Start: 01:32
End: 04:41
Clone: 06:50

Script:-
========

function f_piece
{
t=$1 # The full string
d=$2 # Delimiter
s=$3 # Start
e=$4 # End
if [[ $e = "" ]]; then e=$s; fi
r=`echo $t | cut -d${d} -f$s-$e 2> /dev/null`
if [[ $r = "" ]]; then r=$t; fi
r=$(print $r|tr -s "~" " " )
print $r
}

function f_findmultistring
{
# abc = file name
mstring=$1
num=$(grep -c $mstring abc)
mst=$(grep -e $mstring abc)
mst=$(print $mst|tr -s " " "~" )
integer i=1
integer k=0
integer w=0
while ((i <= $num));
do
if [[ $i = 1 ]]; then
((k = i + 0))
((w = i + 1))
else
((k = w + 1))
((w = k + 1))
fi
f_piece $mst "~" $k $w
((i = i + 1));
done
}
#===========================================
# Start Script
#===========================================
str="Start"
f_findmultistring $str
print ""

str="End"
f_findmultistring $str
print ""

str="Clone"
f_findmultistring $str
print
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top