•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
Assuming "127" as pattern to match and an input as the one posted above:
Note that $1=$1... will recalculate the current record and squeeze repeated FS characters.
Use nawk or /usr/xpg4/bin/awk on Solaris.
awk '/127/{$1=$1c;c++}1' A.txt>B.txtNote 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.
•
•
Join Date: Nov 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How to replace HDD for Toshiba Satellite 2435-S255 ? (Storage)
- Excel X - pasting into find or replace dialog? (Mac Software)
- Jscript Replace Function (ASP)
- ASP Replace Function.... (ASP)
- Find and Replace (Database Design)
- new windosw xp full install edition; won't replace existing Windows Me OS (Windows NT / 2000 / XP / 2003)
Other Threads in the Shell Scripting Forum
- Previous Thread: After my script does ftp, it can't delete the files - URGENT
- Next Thread: need help with writing shell script



Linear Mode