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

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.

Thanx for the reply.

I can't get the number of occurrences right in sed.

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...

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.

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 :confused: .

What I have is this (more real example):

- file A.txt -

[-$1-]
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? :icon_idea:

Sed is not the right tool for this:

awk '/^127/{sub(/^127\./,"&"c" ");c++}1' A.txt

Thank you,

:icon_cheesygrin: :icon_biggrin: :icon_cheesygrin:


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

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

Thanx,

I will try it again tommorow to see...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.