LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-06-2006, 02:08 PM   #1
hfawzy
Member
 
Registered: Aug 2002
Location: Egypt
Distribution: Debian Sarge, Slackware 10.0
Posts: 163

Rep: Reputation: 30
sed trouble


Hi,
I'm having some troubles replacing a string in a file using sed.
Here is what I want : I want to replace the first match occured with 1 the second match with 2, etc... till the end of the file.
For example if the file looks like this :
Quote:
Sample text..
Here is some text...
Some text
I want it to look like this after applying the above replacing system to string "text":
Quote:
Sample 1..
Here is some 2...
Some 3
How can I do that ?
Thank you.
 
Old 08-06-2006, 02:32 PM   #2
sdexp
Member
 
Registered: Sep 2003
Location: USA
Distribution: Ubuntu Linux
Posts: 103

Rep: Reputation: 15
Code:
i=1
for word in `echo "Sample text..
Here is some text...
Some text"`
do 
   echo $word | sed 's/text/$i/g'
   if [ $word = "text" ]
   then
      let "i+=1"
   fi
done
I haven't tested it, but think it will work, maybe with a few modifications.

Last edited by sdexp; 08-07-2006 at 10:36 PM.
 
Old 08-06-2006, 03:20 PM   #3
hfawzy
Member
 
Registered: Aug 2002
Location: Egypt
Distribution: Debian Sarge, Slackware 10.0
Posts: 163

Original Poster
Rep: Reputation: 30
Thanks for the reply. This almost solved the problem.
Problem is that $i incrments, even if the word is not "text". I want to increment $i only if "text" is found and has been replaced.
 
Old 08-06-2006, 05:21 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Will this work for you?
Code:
perl -ne 'print if /text/' < file.txt | perl -pe 's/text/$./'
 
Old 08-06-2006, 05:36 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I think this is what hfawzy wants. It makes a backup of the original file.
Code:
perl -i.bak -e '$i=1; while(<>){$i++ if(s/text/$i/);print}' filename
 
Old 08-07-2006, 10:37 PM   #6
sdexp
Member
 
Registered: Sep 2003
Location: USA
Distribution: Ubuntu Linux
Posts: 103

Rep: Reputation: 15
Yes, you caught me dosing off there hfawzy.

I did correct it though in that post.

Here it is again:

Code:
i=1
for word in `echo "Sample text..
Here is some text...
Some text"`
do 
   echo $word | sed 's/text/$i/g'
   if [ $word = "text" ]
   then
      let "i+=1"
   fi
done
 
  


Reply



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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
Having some trouble with sed P0ldy Linux - General 6 01-04-2006 04:26 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:06 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