User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,439 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,613 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 1558 | Replies: 10 | Solved
Reply
Join Date: Nov 2007
Posts: 6
Reputation: Gorilatsouk1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gorilatsouk1 Gorilatsouk1 is offline Offline
Newbie Poster

Question Replace

  #1  
Nov 4th, 2007
Hello guys,

I have tried unsuccessfully the following problem with sed and awk
grep commands:

- Try to find a specific word in a text file, and when you find it,
add after that word the number of occurrence of that word,
but leave the first match unchanged, i.e. no addition of the number
for the first match.


For example:

- file A.txt -
122.
123.
124. This
125. is
126. a
127. nice
127. but
127. cold
127. and
127. windy
128. evening.
129.
130.

should become
- file B.txt -
122.
123.
124. This
125. is
126. a
127. nice
127.1 but
127.2 cold
127.3 and
127.4 windy
128. evening.
129.
130.

A solution will be highly appreciated, guys.

Thank you in advance
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Replace

  #2  
Nov 4th, 2007
Sed can do it easily. You'll have to get a little familiar with some simple regular expressions.

Here's a page with all kinds of useful information.

Hope this helps.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: Gorilatsouk1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gorilatsouk1 Gorilatsouk1 is offline Offline
Newbie Poster

Re: Replace

  #3  
Nov 4th, 2007
Thanx for the reply.

I can't get the number of occurrences right in sed.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Replace

  #4  
Nov 4th, 2007
Hmm, you're right. I was thinking of the = operator in sed, which is just a line count...
I'm quite rusty at this stuff... so give me a little bit to find out what I can...
Reply With Quote  
Join Date: Feb 2007
Posts: 52
Reputation: radoulov is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Replace

  #5  
Nov 5th, 2007
Assuming "127" as pattern to match and an input as the one posted above:

awk '/127/{$1=$1c;c++}1' A.txt>B.txt

Note that $1=$1... will recalculate the current record and squeeze repeated FS characters.
Use nawk or /usr/xpg4/bin/awk on Solaris.
Last edited by radoulov : Nov 5th, 2007 at 6:16 am.
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: Gorilatsouk1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gorilatsouk1 Gorilatsouk1 is offline Offline
Newbie Poster

News Re: Replace

  #6  
Nov 5th, 2007
Hi radoulov,

Thanx for that:
awk '/127/{$1=$1c;c++}1' A.txt>B.txt

But,
awk destroys my spaces and tabs between field $1 and $2.
This is why I tried to do it with sed, but requires little bit more
advance knowledge on scripting with sed which I dont have .

What I have is this (more real example):

- file A.txt -

[-$1-][----------$2-----------]
122.
123.
124. CASE A IS
125.
126.__WHEN 0 DO B=A;
127.
127. __WHEN 1 DO
127._________ B=B+1;
127. __WHEN 2 DO
127._________ B=B+2;
127.
128. OTHERWISE DO;
129.
130.ESAC;

i.e. I have added after line 127 some more cases, which I want
to indicate by numbering them after line, e.g. 127.x (where x the number of repetition so far from the original line), but without changing the text
format of the other words present in the same line.

Any ideas?
Last edited by Gorilatsouk1 : Nov 5th, 2007 at 1:45 pm.
Reply With Quote  
Join Date: Feb 2007
Posts: 52
Reputation: radoulov is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Replace

  #7  
Nov 5th, 2007
Sed is not the right tool for this:

awk '/^127/{sub(/^127\./,"&"c" ");c++}1' A.txt
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: Gorilatsouk1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gorilatsouk1 Gorilatsouk1 is offline Offline
Newbie Poster

Re: Replace

  #8  
Nov 6th, 2007
Thank you,




I tested it succccesfully on Suse/Linux
but I can't make it work in Solaris, in which I need it (nawk version?).
Any alterantives for Solaris are welcome.

Thank you, once more
Reply With Quote  
Join Date: Feb 2007
Posts: 52
Reputation: radoulov is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: Replace

  #9  
Nov 6th, 2007
On Solaris it should work with nawk and /usr/xpg4/bin/awk.

Tested with

/usr/bin/nawk:
SunOS 5.8 Generic 111111-04 Mar 2004

/usr/xpg4/bin/awk:
SunOS 5.8 Generic February 2000
Reply With Quote  
Join Date: Nov 2007
Posts: 6
Reputation: Gorilatsouk1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gorilatsouk1 Gorilatsouk1 is offline Offline
Newbie Poster

Re: Replace

  #10  
Nov 6th, 2007
Thanx,

I will try it again tommorow to see...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC