sed:替换方括号之间的字母 [英] sed: replace letter between square brackets

查看:112
本文介绍了sed:替换方括号之间的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

signal[i]
signal[bg]
output [10:0]
input [i:1]

我想要的是替换方括号之间的字母(例如通过下划线)并保留代表表声明的其他字符串:

what I want is to replace the letters between square brackets (by underscore for example) and to keep the other strings that represents table declaration:

signal[_]
signal[__]
output [10:0]
input [i:1]

谢谢

推荐答案

尝试:

awk '{gsub(/\[[a-zA-Z]+\]/,"[_]")} 1'  Input_file

全局替换(括号)字母直到它们最长的匹配然后用 ​​[_].提及 1 将打印行(已编辑或未编辑).

Globally substituting the (bracket)alphabets till their longest match then with [_]. Mentioning 1 will print the lines(edited or without edited ones).

以上将用一个 _ 替换所有字母,因此要获得尽可能多的下划线,以下可能有帮助.

Above will substitute all alphabets with one single _, so to get as many underscores as many characters are there following may help in same.

awk '{match($0,/\[[a-zA-Z]+\]/);VAL=substr($0,RSTART+1,RLENGTH-2);if(VAL){len=length(VAL);;while(i<len){q=q?q"_":"_";i++}};gsub(/\[[a-zA-Z]+\]/,"["q"]")}1'   Input_file

awk '{
        match($0,/\[[a-zA-Z]+\]/);
        VAL=substr($0,RSTART+1,RLENGTH-2);
        if(VAL){
                len=length(VAL);
                while(i<len){
                                q=q?q"_":"_";
                                i++
                            }
               };
        gsub(/\[[a-zA-Z]+\]/,"["q"]")
     }
     1
    '   Input_file

很快会添加说明.

以下是针对 OP 和用户的解释说明.

Following is the one with explanation purposes for OP and users.

awk '{
        match($0,/\[[a-zA-Z]+\]/);            #### using match awk's built-in utility to match the [alphabets] as per OP's requirement.
        VAL=substr($0,RSTART+1,RLENGTH-2);    #### Creating a variable named VAL which has substr($0,RSTART+1,RLENGTH-2); which will have substring value, whose starting point is RSTART+1 and ending point is RLENGTH-2.
                                                   RSTART and RLENGTH are the variables out of the box which will be having values only when awk finds any match while using match.
        if(VAL){                              #### Checking if value of VAL variable is NOT NULL. Then perform following actions.
                len=length(VAL);              #### creating a variable named len which will have length of variable VAL in it.
                while(i<len){                 #### Starting a while loop which will run till the value of VAL from i(null value).
                                q=q?q"_":"_"; #### creating a variable named q whose value will be concatenated it itself with "_".
                                i++           #### incrementing the value of variable i with 1 each time.
                            }
               };
        gsub(/\[[a-zA-Z]+\]/,"["q"]")         #### Now globally substituting the value of [ alphabets ] with [ value of q(which have all underscores in it) then ].
     }
     1                                        #### Mentioning 1 will print (edited or non-edited) lines here.
    '  Input_file                             #### Mentioning the Input_file here.

这篇关于sed:替换方括号之间的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆