首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tcl:如何在多行字符串中有条件地替换

Tcl:如何在多行字符串中有条件地替换
EN

Stack Overflow用户
提问于 2012-10-15 06:20:39
回答 1查看 949关注 0票数 2

假设我有一个很大的字符串,假设有1000行,并将该字符串命名为"itext“。

我只对"itext“中包含单词"Description:”的那些行感兴趣。例如,仅在原始"itext“字符串中包含字符串"Description:”的行上进行以下替换:

代码语言:javascript
复制
regsub -all -- {Loopback} $itext "Lo" itext ;

有没有什么方法可以做到这一点(不需要逐行进行,拆分和连接)?

例如,"itext“可以是这样的:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-15 09:27:49

您只需使用-line选项即可启用换行符敏感匹配:

代码语言:javascript
复制
regsub -all -line -- {^(Description:.*)Loopback} $itext {\1Lo} itext

http://tcl.tk/man/tcl8.5/TclCmd/regsub.htm

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12887124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档