Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


conditional string substitution

conditional string substitution

conditional string substitution

Hello All,

Given that I have a huge string, let's say 1000 lines,
and let's name that string like "itext".

And I am only interested in those lines in "itext" that contain the word "Description:".
And make, for example, the following substitution ONLY on the line containing the string "Description:" :
regsub -all -- {Loopback} $itext "Lo" itext ;

Is there any way to accomplish this (without going line by line, splitting and joining) ?

For example, "itext" can be something like this :

Loopback11 is up, line protocol is up
Hardware is Loopback
Description: Loopback11-Eigrp-National
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Auto-duplex, Auto-speed
input flow-control is off, output flow-control is off
ARP type: ARPA, ARP Timeout 04:00:00
Last input never, output never, output hang never
Last clearing of "show interface" counters never
Input queue: 0/2000/20036/0 (size/max/drops/flushes); Total output drops: 242246
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 0 bits/sec, 0 packets/sec
159767635870 packets input, 108501870627241 bytes, 0 no buffer
Received 9361144 broadcasts (8029769 multicasts)
0 runts, 20036 giants, 0 throttles
20036 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 0 multicast, 0 pause input
0 input packets with dribble condition detected
200041278050 packets output, 193147599625066 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier, 0 pause output
0 output buffer failures, 0 output buffers swapped out

RE: conditional string substitution

For multi-line mode try to use the -lineanchor switch of regsub

RE: conditional string substitution

On the other hand, if the occurrence of the target text is always going to be of the form: "Description: Loopback", you could use the string map function:

CODE -->

set itext [string map {"Description: Loopback" "Description: Lo"} $itext] 

Bob Rashkin

RE: conditional string substitution

regsub -all -line -- {^(Description:.*)Loopback} $itext {\1Lo} itext

is the solution :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close