sed very confused

Hello experts,
I have this complicated code that output value in between pattern and add "a string" to the front of the output.
The problems I have many pattern that are the same.
so I want to know how to get the value between 1st pattern, 2nd pattern etc.
Any suggestions?

sed -n '/<pattern>/s/.*>\(.*\)<.*/a string\1/gp' file

file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

Please post examples.

Jean-Pierre.

ok

so
file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

original sed command:
sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file

sed -n '3,/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file

hm.. I tried this one before, it output all values up to the 1st pattern.

so
file:
<before>blah<\before>
<before>blah1<\before>
<before>blah2<\before>
<Pattern>file<\Pattern>
..
<Pattern>file2<\Pattern>

outputs:

string=blah
string=blah1
string=blah2
string=file

awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' file

Regards

The same thing that Franklin52 :

awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' file

Jean-Pierre.

mm..I'm not very familiar with awk, but it's not giving what I want.
it outputs something like:

string=\Pattern>
string=
string=

mm..I get an empty output.

No problem on my AIX box.

$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

$ awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' pattern.txt
string=file3
$

Try with nawk instead of awk.

@Franklin52
Your solution prints lines from the 3rd occurence of pattern to the end of the file (I haven't detect the problem when i first read your post).

Jean-Pierre.

THANK YOU~~~~~~~~~~~~~~~~~~~~~~~~~~
:wink:

It works fine for me, did you try it? It prints only if c==3 but with an exit statement it's faster for a big file.

Regards

Your command prints line for the 3rd occurrence of Pattern and for all following lines until next occurence of Pattern.
Adding the exit statement resolves the problem.

$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
.. 
...
....<Pattern>file4<\Pattern>

$ awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' pattern.txt
string=file3
string=
string=
string=
$ awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3;exit}' pattern.txt
string=file3
$

Jean-Pierre.

OK, sorry your right, I misread the question, I thought he wants to print everything between 2 patterns.

Regards