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
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 07:13 PM
how to find a position and print some string in the next and same position naveenkcl Shell Programming and Scripting 1 08-21-2008 01:18 PM
Find and replace character in a string callimaco0082 UNIX for Dummies Questions & Answers 7 04-10-2008 10:47 AM
how to find substring position in a given string balareddy Shell Programming and Scripting 2 01-29-2007 10:06 AM
how to find and replace string mridula High Level Programming 3 08-17-2006 04:44 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 01-21-2009
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Unhappy Find the position of a string and replace with another string

Hi,

I have a file named "Test_2008_01_21"
The file contains a string "manual" that occurs many times in the file
How can i find the positions of the string "manual" in the file

Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string the " manual " with another string "annual".

How it can be done?

Some help please
Regards
bab123
  #2 (permalink)  
Old 01-21-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
Code:
echo "leoisleoisleo" | sed 's/leo/LEO/2'
  #3 (permalink)  
Old 01-21-2009
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Thanks for the reply
If know thw number of occurances of string I can replace the string
BUt if i want to know how many times the string appears?
What are the positions of string appearances?
Please suggest me

regards
bab123
  #4 (permalink)  
Old 01-21-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
  
 

Join Date: Jul 2008
Location: Philippines
Posts: 125
Code:
echo "leoisleoisleo" | grep -on leo
Code:
echo "leoisleoisleo" | grep -on leo | cut -d: -f1
Code:
 echo "leoisleoisleo" | grep -on leo | wc -l
  #5 (permalink)  
Old 01-21-2009
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
I am getting the following error

grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
  #6 (permalink)  
Old 01-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by bab123 View Post
I am getting the following error

grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

The -o option to grep is not standard.

But there is no way to do what you want just using grep; it cannot replace anything.
  #7 (permalink)  
Old 01-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by bab123 View Post
I have a file named "Test_2008_01_21"
The file contains a string "manual" that occurs many times in the file
How can i find the positions of the string "manual" in the file

Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string the " manual " with another string "annual".

How it can be done?

Assuming there is only one instance of "manual" one a line:

Code:
awk '
       {x += sub( "manual", "manual" )}
x == 2 {x += sub( "manual", "annual" ) }
       { print }' Test_2008_01_21
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 04:57 AM.


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