LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-05-2008, 12:48 AM   #1
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Rep: Reputation: 15
SED replace string by occurrence


hi all,
I have a text file with following content

PAGENUMBER
asasasa
asasasa
PAGENUMBER
sasasasasa
PAGENUMBER

using sed i want to replace PAGENUMBER by occurrence count

eg

1
asasasa
asasasa
2
sasasasasa
3


Thanks and Regards,
uttam hoode
 
Old 03-05-2008, 01:06 AM   #2
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Not sed, but awk... works with your sample data.
Code:
awk '/PAGENUMBER/{$0=++i}1' file
If you need the code explained, or need me to write an easier to understand one just ask.
 
Old 03-05-2008, 02:23 AM   #3
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Original Poster
Rep: Reputation: 15
this is working fine.
how can i replace the string present in the file by output of awk without using redirection (>). In sed its called inline edit. Is it possible with awk?
 
Old 03-05-2008, 06:09 AM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by uttam_h View Post
this is working fine.
how can i replace the string present in the file by output of awk without using redirection (>). In sed its called inline edit. Is it possible with awk?
No,
why you don't want to use redirection?

Anyway:

Code:
{ rm file&&awk >file '$0=/^PAGENUMBER/?++c:$0';}<file
 
Old 03-05-2008, 07:59 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by uttam_h View Post
Is it possible with awk?
No, but it is in Perl as the -i flag. E.g.,
Code:
perl -pi -e 's/PAGENUMBER/++$a/e' file
 
Old 03-05-2008, 10:02 PM   #6
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks angrybanana,radoulov and osor for the reply.

I will use the solution given by osor.

perl -pi -e 's/PAGENUMBER/++$a/e' file

Thanks and Regards,
uttam hoode
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
How to replace all occurances of a complex string using sed 6millionbucks Programming 6 08-12-2007 11:55 AM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM


All times are GMT -5. The time now is 12:16 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration