Matching Line After Pattern (Pattern Occurs Multiple Times)

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by rew.. » Mon, 12 Sep 2005 03:17:39



Note: Cross Post from comp.unix.misc.

Hello, I have a sorted list that contains dates:

1/1/2005
1/1/2005
1/8/2005
1/10/2005
1/10/2005
1/10/2005
1/15/2005
1/21/2005
1/21/2005

I need a one liner that matches the line after the last occurance of
the pattern to the end of the file. So in the example if the regex was
1\/10\/2005, it would match after the 3rd occurence of 1/10/2005; the
output would contain 3 lines starting at 1/15/2005.

I have tried: sed '1,'1\/10\/2005'/d, but that prints the 2nd occurance
to the end of the file. I have also looked around the newsgroups and
have seen similar functionality in AWK, GREP and PERL, but I have not
stumbled into the solution.

I would appreciate it if you could point me in the right direction.

TIA

 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by Loki Harfag » Mon, 12 Sep 2005 03:42:15



Quote:> Note: Cross Post from comp.unix.misc.

> Hello, I have a sorted list that contains dates:

> 1/1/2005
> 1/1/2005
> 1/8/2005
> 1/10/2005
> 1/10/2005
> 1/10/2005
> 1/15/2005
> 1/21/2005
> 1/21/2005

> I need a one liner that matches the line after the last occurance of
> the pattern to the end of the file. So in the example if the regex was
> 1\/10\/2005, it would match after the 3rd occurence of 1/10/2005; the
> output would contain 3 lines starting at 1/15/2005.

> I have tried: sed '1,'1\/10\/2005'/d, but that prints the 2nd occurance
> to the end of the file. I have also looked around the newsgroups and
> have seen similar functionality in AWK, GREP and PERL, but I have not
> stumbled into the solution.

> I would appreciate it if you could point me in the right direction.

  Would this be OK or do you really insist on
having a *one only* sed expression ?

$ tac yourfile | sed '/1\/10\/2005/q' | sed
'/1\/10\/2005/d'   |tac

 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by Loki Harfag » Mon, 12 Sep 2005 03:45:35


Le Sat, 10 Sep 2005 20:42:15 +0200, Loki Harfagr a crit?:

...

Quote:

>   Would this be OK or do you really insist on
> having a *one only* sed expression ?

> $ tac yourfile | sed '/1\/10\/2005/q' | sed
> '/1\/10\/2005/d'   |tac

  Or in just one expression, though there still is the tac|tac ...

$ tac patterned.txt | sed '/1\/10\/2005/Q'  |tac

 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by Pawel Gancar » Mon, 12 Sep 2005 03:49:27



Quote:>   Would this be OK or do you really insist on
> having a *one only* sed expression ?

> $ tac yourfile | sed '/1\/10\/2005/q' | sed
> '/1\/10\/2005/d'   |tac

*one only*? Here you are:
$ tac yourfile | sed '/1\/10\/2005/,$d' | tac
regards
--
pgancarz, at, o2, pl
 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by William Jame » Mon, 12 Sep 2005 04:29:35



> Note: Cross Post from comp.unix.misc.

> Hello, I have a sorted list that contains dates:

> 1/1/2005
> 1/1/2005
> 1/8/2005
> 1/10/2005
> 1/10/2005
> 1/10/2005
> 1/15/2005
> 1/21/2005
> 1/21/2005

> I need a one liner that matches the line after the last occurance of
> the pattern to the end of the file. So in the example if the regex was
> 1\/10\/2005, it would match after the 3rd occurence of 1/10/2005; the
> output would contain 3 lines starting at 1/15/2005.

awk '{s=s $0 RS}/^1\/10\/2005/{s=""}END{print s}'
 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by Ed Morto » Mon, 12 Sep 2005 05:39:48



> Note: Cross Post from comp.unix.misc.

> Hello, I have a sorted list that contains dates:

> 1/1/2005
> 1/1/2005
> 1/8/2005
> 1/10/2005
> 1/10/2005
> 1/10/2005
> 1/15/2005
> 1/21/2005
> 1/21/2005

> I need a one liner that matches the line after the last occurance of
> the pattern to the end of the file.

$ awk '/1\/10\/2005/{f=1;next}f' file
1/15/2005
1/21/2005
1/21/2005

Regards,

        Ed.

 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by Enrique Perez-Terro » Mon, 12 Sep 2005 09:31:04




Quote:> Note: Cross Post from comp.unix.misc.

> Hello, I have a sorted list that contains dates:

> 1/1/2005
> 1/1/2005
> 1/8/2005
> 1/10/2005
> 1/10/2005
> 1/10/2005
> 1/15/2005
> 1/21/2005
> 1/21/2005

> I need a one liner that matches the line after the last occurance of
> the pattern to the end of the file. So in the example if the regex was
> 1\/10\/2005, it would match after the 3rd occurence of 1/10/2005; the
> output would contain 3 lines starting at 1/15/2005.

> I have tried: sed '1,'1\/10\/2005'/d, but that prints the 2nd occurance
> to the end of the file. I have also looked around the newsgroups and
> have seen similar functionality in AWK, GREP and PERL, but I have not
> stumbled into the solution.

sed -e '1,/1\/10\/2005/d' -e '/1\/10\/2005/d' filename

-Enrique

 
 
 

Matching Line After Pattern (Pattern Occurs Multiple Times)

Post by rew.. » Tue, 13 Sep 2005 08:42:05


All work. Many solutions to choose from. Thanks everyone.

Regards,
Bob

 
 
 

1. Pattern matching and extracting the data which matches the pattern

Help,

I am running Solaris 2.6, and have only ever written scripts for the Korn
Shell......and have the following query......

I have a very long text file ( each line represents a CAD model name ).
Somewhere within the model name (i.e. line of the text file ) , is  the
drawing reference number......this number will always match a particular
pattern of letters, hyphens and numbers - which will not be matched
elsewhere within the remainder of the line.
I need a mechanism to search each line in turn, match the pattern of
letters, hyphens and numbers and extract purely that portion of the line and
write it to a separate file. My problem , is that the pattern could appear
anywhere within each line and the number of fields can differ ( if it was
always in the same place or had a constant number of fields, I would use
'cut' )
For example, an extract from the input file could be :-

LD100 WEIGHT DAMPER              W7A   22 Z  535210043-A  USE30SE99MVS
99/10/26
LD100 GROMMET  GUIDE  ROD          W5D   12 Z  545180058-B  USE30SE99MAK
99/10/26
LD100 GROMMET  GUIDE  ROD          W5D   22 Z  545180058-B  USE30SE99MAK
99/10/26
LD100 BRACKET-CABLE,SELECT       W7A   12 Z  535210034-C  USE06SE99LJL
99/10/26
LD100 BRACKET-CABLE,SELECT       W7A   22 Z  535210034-C  USE06SE99LJL
99/10/26

And I need to extract the data matching the following pattern: nine
numbers,a hypen and a letter and put it in an output file - thus the output
I would like to see is :-

535210043-A
545180058-B
545180058-B
535210034-C
535210034-C

Many thanks in advance for your help

Mark Hounslow
LDV Limited

2. Complete P133 Computer System at Low Price

3. matching pattern over multiple lines

4. Loginhog problem.

5. Pattern Matching Multiple Lines with SED and AWK

6. COMPAQ LTE5400

7. Matching a pattern in a file and inserting variable string above the line matched?

8. need a 'port proxy' daemon

9. Pattern Matching Not Working When Pattern Assign To A Variable

10. ksh pattern matching when pattern is in a variable

11. inserting lines before and after lines with matching pattern using sed?

12. grepping for pattern, and then deleting ALL the following lines after the pattern

13. matching lines of pattern in a file