LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-05-2011, 03:50 AM   #1
mbaste2
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Rep: Reputation: 0
output the occurence number in sed or grep results in every regex match


i have a large plain text kml file (example below) :

<Polygon>
<coordinates>
120,14
121,14
...
...
...
121,13
</coordinates>
</Polygon>
<Polygon>
<coordinates>
130,15
131,15
...
...
...
131,14
</coordinates>
</Polygon>

where i need to replace every line containing <Polygon> with
<!-- n --><Polygon>
where n is the nth occurence of the match in the entire file.

search results i have encountered teach only how to replace the (say) fifth occurence of a match in the file. what i need is to display the nth occurence everytime there is a match.

is this possible? the file is very large.

any help, suggestion, recommendation welcome. thanks.
 
Old 04-05-2011, 04:43 AM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Ruby(1.9+)

Code:
$ ruby -pne 'BEGIN{c=0}; c+=1 if /<Polygon>/; $_.gsub!(/(<Polygon>)/,"<!-- #{c} --> \\1" ) if /<Polygon>/ ' file
<!-- 1 --> <Polygon>
<coordinates>
120,14
121,14
...
...
...
121,13
</coordinates>
</Polygon>
<!-- 2 --> <Polygon>
<coordinates>
130,15
131,15
...
...
...
131,14
</coordinates>
</Polygon>
 
Old 04-05-2011, 10:50 AM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Code:
awk '/<Polygon>/{printf "<!-- "++n" -->"}1' infile > outfile
 
Old 04-06-2011, 01:58 AM   #4
mbaste2
LQ Newbie
 
Registered: Nov 2009
Posts: 3

Original Poster
Rep: Reputation: 0
thanks to both replies.
awk worked as expected.
i'm sure that will work in ruby too, but i don't know ruby yet.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Any grep, sed or awk gurus with regex familiarity? I need some help. bcrawl Linux - Newbie 19 01-19-2011 07:52 PM
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 04:33 PM
sed - last occurence of a match mr_scary Linux - Desktop 7 02-13-2009 12:44 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration