- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Matching first line iwth a specific string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 04:17 AM
тАО03-10-2006 04:17 AM
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======
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 04:24 AM
тАО03-10-2006 04:24 AM
Re: Matching first line iwth a specific string
# 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...
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 04:26 AM
тАО03-10-2006 04:26 AM
Re: Matching first line iwth a specific string
$cat test | sed -e '1s/Mar 1/\
&\<\/h1\> /'
Mar 1
Mar 1
Mar 2
Mar 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 04:28 AM
тАО03-10-2006 04:28 AM
Re: Matching first line iwth a specific string
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
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 04:59 AM
тАО03-10-2006 04:59 AM
Re: Matching first line iwth a specific string
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!
- Tags:
- Sort
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 05:07 AM
тАО03-10-2006 05:07 AM
SolutionIt 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 05:09 AM
тАО03-10-2006 05:09 AM
Re: Matching first line iwth a specific string
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 05:11 AM
тАО03-10-2006 05:11 AM
Re: Matching first line iwth a specific string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-10-2006 07:35 AM
тАО03-10-2006 07:35 AM
Re: Matching first line iwth a specific string
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-13-2006 03:39 AM
тАО03-13-2006 03:39 AM
Re: Matching first line iwth a specific string
I get a `(' unexpected error when running that line of perl.
Is there a ( missing?