The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 2 04-11-2008 11:32 AM
Replace second occurrence only lyoncc Shell Programming and Scripting 5 12-26-2007 07:21 PM
last occurrence of a string across multiple files porphyrin UNIX for Dummies Questions & Answers 2 12-24-2007 05:39 AM
delete string in a text file leaving the first occurrence gopskrish UNIX for Dummies Questions & Answers 12 10-31-2006 06:32 AM
Search and replace to first occurrence of string gilmord UNIX for Dummies Questions & Answers 7 05-03-2006 04:43 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2008
Registered User
 

Join Date: Oct 2007
Posts: 17
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Feb 2008
Posts: 19
Quote:
Originally Posted by uttamhoode View Post
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
why not use awk, try this:
cat $filename | awk '/PAGENUMBER/{count++; { sub("PAGENUMBER",count, $0)}; }; {print }'

A part of this command is taken from a similar post by lyoncc, thanks to lyoncc.
Reply With Quote
  #3 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,448
Code:
awk '/PAGENUMBER/{print ++i;next}1' file
Reply With Quote
  #4 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Oct 2007
Posts: 17
how can i update the result in the same file? (inline edit)
Reply With Quote
  #5 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,448
redirect to another file, rename it back to the original.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0