The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 9 06-08-2009 06:11 AM
Replace second occurrence only lyoncc Shell Programming and Scripting 5 12-26-2007 11:21 PM
last occurrence of a string across multiple files porphyrin UNIX for Dummies Questions & Answers 2 12-24-2007 09:39 AM
delete string in a text file leaving the first occurrence gopskrish UNIX for Dummies Questions & Answers 12 10-31-2006 10:32 AM
Search and replace to first occurrence of string gilmord UNIX for Dummies Questions & Answers 7 05-03-2006 07:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-05-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
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
  #2 (permalink)  
Old 03-05-2008
sank sank is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 24
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.
  #3 (permalink)  
Old 03-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
awk '/PAGENUMBER/{print ++i;next}1' file
  #4 (permalink)  
Old 03-05-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
how can i update the result in the same file? (inline edit)
  #5 (permalink)  
Old 03-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
redirect to another file, rename it back to the original.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0