Operating System - Linux
1753964 Members
7418 Online
108811 Solutions
New Discussion юеВ

Re: Matching first line iwth a specific string

 
SOLVED
Go to solution
OFC_EDM
Respected Contributor

Matching first line iwth a specific string

Mar 1
Mar 1
Mar 2
Mar 2

Above is a simplified version of the text I'm processing.

I want to do is surround the Month and Day with a tag "

" and "

" but only for the first occurrence.

Below is the command I've tried but it's putting the tags around ALL occurrences of "Mar 1"?

How do I make it just match the 1st occurence?

cat test|sed -e '1,$s/Mar 1/\

&\<\/h1\> /'

========== START OUTPUT======

Mar 1


Mar 1


Mar 2
Mar 2
========== END OUTPUT======
The Devil is in the detail.
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor

Re: Matching first line iwth a specific string

Hi Kevin:

# sed -e '1,1s/Mar 1/\

&\<\/h1\> /' file

Note that you don't have to spawn a separate process to first read the file (i.e. the 'cat' piped to 'sed').

Regards!

...JRF...

Carlos Roberto Schimidt
Regular Advisor

Re: Matching first line iwth a specific string

Dont use "$", see above

$cat test | sed -e '1s/Mar 1/\

&\<\/h1\> /'


Mar 1


Mar 1
Mar 2
Mar 2
Victor Fridyev
Honored Contributor

Re: Matching first line iwth a specific string

Hi,

For the exact input as in your msg, I'd try the following:

awk 'BEGIN {flag=""}
{if(flag!=$0){print "

",$0,"

";flag=$0}
else print $0}' inputfile

HTH

Entities are not to be multiplied beyond necessity - RTFM
Sandman!
Honored Contributor

Re: Matching first line iwth a specific string

Kevin,

How about sorting the input first and then piping the result to awk which uses associative arrays for parsing and replacing the string you want.

# sort -k1,2 inp | awk '{if(prev!=$1$2){print b$1,$2e;prev=$1$2}else print $0}' b="

" e="

"

cheers!
James R. Ferguson
Acclaimed Contributor
Solution

Re: Matching first line iwth a specific string

Hi (again) Kevin:

It occurs to me that I took your post a bit too literally.

Should you want only the first matching line of *any* block, as for example to substitute about "Mar 2":

Mar 1
Mar 1

Mar 2


Mar 2
Mar 3

...then:

# sed -e 's/Mar 2/\

&\<\/h1\>/;n' file

This works for the original case too, but is appropriately general to the span of the entire file.

Regards!

...JRF...

OFC_EDM
Respected Contributor

Re: Matching first line iwth a specific string

I wasn't clear enough.

Basically I'm putting in Headers for each Day of a month from a log.

So using my previous example the output would be:

Mar 1


Mar 1

Mar 2


Mar 2
etc...for the rest of the days.

So really my pattern to match is
"Mar [ 0-9][0-9]" which would match Mar 1 to Mar 31.

Preceding the search with 1 (1s/Mar 1/)just matches the first line input and not the first occurence so that's not doing what I need.

Any more thoughts?
The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: Matching first line iwth a specific string

Thanks James. I was surprised to find your answer posted prior to my attempted clarification.
The Devil is in the detail.
James R. Ferguson
Acclaimed Contributor

Re: Matching first line iwth a specific string

Hi (again) Kevin:

Well, the 'sed' solution I posted worked for the particular repetition of lines you posted, but it *fails* for more generalized matching. I should have gone straight for Perl.

Consider a file that looks like:

Mar 1
Mar 1
Mar 1
Mar 2
Mar 2
Mar 2
Mar 2
Mar 3
Mar 4
the end at Mar 4 the end!

With the 'sed' solution:

# sed -e 's/Mar [0-9[0-9]/\

&\<\/h1\>/;n' file

...you get:

Mar 1


Mar 1

Mar 1


Mar 2

Mar 2


Mar 2

Mar 2


Mar 3

Mar 4


the end at Mar 4 the end!

...which is not much use.

*Instead* use:

# perl -ple chomp;unless ($prev=~$_) {s%(.*)(Mar \d+)(.*)%$1

$2

$3%};$prev=$_' file

...and you can have:

Mar 1


Mar 1
Mar 1

Mar 2


Mar 2
Mar 2
Mar 2

Mar 3


Mar 4


the end at

Mar 4

the end!

Regards!

...JRF...
OFC_EDM
Respected Contributor

Re: Matching first line iwth a specific string

Hi JRF

I get a `(' unexpected error when running that line of perl.

Is there a ( missing?

The Devil is in the detail.