Home > Enterprise >  How do I abstrct two or more substring in one single line In shell script
How do I abstrct two or more substring in one single line In shell script

Time:03-17

I need to write a shell script to replace string to a series number in TS file, and those string are contained in a function, in fact they are the first parameter of the function, thus I tried to find all substring formed by this function name, my question is, there might be two or more function called in one single line, and I use cat command to read TS file then I tried

localize=`cat MailDetailLayer.ts | grep -o GetLocalize\(*` 
for i in $localize
do
echo $i
done

but it only returns every "GetLocalize(" to me, then I tried

localize=cat MailDetailLayer.ts 
for i in $localize
do
echo $i
    if [[ $i == *"GetLocalize(\""* ]];then
        echo $i
        delper=${i#*\"}
        
        delbehand=${delper%%\"*}
        echo $delbehand
        let flag  
        echo $flag
    fi

done

but it will skip blankspace that's not right. I need to abstrct every string like: "ahfjkhfak" in GetLocalize("ahfjkhfak", xx, yy)

My TS file like this:

class MailDetailLayer extends BaseLayerScript<VMMailDetail> {
    protected static _resPath = "mail/com_read"

    protected _mailInfo : IEmailInfo

    constructor(layer: ILayer, initData: {mailInfo : IEmailInfo}) {
        super(layer, initData)
        this._mailInfo = initData.mailInfo
    }

    protected provideVMData(vm: VMMailDetail) {
        return {
            context : this.formMailContext(),
            attachment : this._mailInfo.attachment 
        }
    }GetLocalize("this is a string")


    async onDelete(){
        let confirmDel = await Game.Common.showMessage(GetLocalize("replace this string to number"), GetLocalize("rasdbhjbvx"), GetLocalize("there might be many GetLocalize functions in one line"))
        if(confirmDel == 1){
            let ret = await Game.Email.delMail(EEmailType.Defult, this._mailInfo.eid) 
            if(ret){
                Game.Email.Data.delMail(this._mailInfo.eid)
                this.Layer.CloseSelf()
            }
        }
    }


    GetLocalize("and this function has other parameters", sfajh, asjhfbhk)
    async onReceiveAttachment(){
        let ret = await Game.Email.receiveAttachment(EEmailType.Defult, this._mailInfo.eid)
        if(ret){
            Game.Email.Data.receiveAttachment(this._mailInfo.eid)
        }
    }

    formMailContext(){
        if(this._mailInfo.eContext){
            return this._mailInfo.eContext
        }
        let context =  GetLocalize(ConfigEntry.Mail.getRowById(this._mailInfo.tid).TextContent)
        GetLocalize("if the first parameter is not a string nothing happens")
        // TODO

        // if(this._mailInfo.specialValueArr){
        //     return GetLocalize(context, ...this._mailInfo.specialValueArr)
        // }
        return  context
    }
}

CodePudding user response:

Doing search-and-replace in the shell is not the right tool: firstly there is no "global" option. I'd suggest a more capable text processing language like perl or python.

For example

perl -pe '
    s{(?<=GetLocalize\(")(.*?)(?=")}
    {
        # your transformation goes here, for example
        reverse uc $1
    }ge
' file.ts

outputs

class MailDetailLayer extends BaseLayerScript<VMMailDetail> {
    protected static _resPath = "mail/com_read"

    protected _mailInfo : IEmailInfo

    constructor(layer: ILayer, initData: {mailInfo : IEmailInfo}) {
        super(layer, initData)
        this._mailInfo = initData.mailInfo
    }

    protected provideVMData(vm: VMMailDetail) {
        return {
            context : this.formMailContext(),
            attachment : this._mailInfo.attachment
        }
    }GetLocalize("GNIRTS A SI SIHT")


    async onDelete(){
        let confirmDel = await Game.Common.showMessage(GetLocalize("REBMUN OT GNIRTS SIHT ECALPER"), GetLocalize("XVBJHBDSAR"), GetLocalize("ENIL ENO NI SNOITCNUF EZILACOLTEG YNAM EB THGIM EREHT"))
        if(confirmDel == 1){
            let ret = await Game.Email.delMail(EEmailType.Defult, this._mailInfo.eid)
            if(ret){
                Game.Email.Data.delMail(this._mailInfo.eid)
                this.Layer.CloseSelf()
            }
        }
    }


    GetLocalize("SRETEMARAP REHTO SAH NOITCNUF SIHT DNA", sfajh, asjhfbhk)
    async onReceiveAttachment(){
        let ret = await Game.Email.receiveAttachment(EEmailType.Defult, this._mailInfo.eid)
        if(ret){
            Game.Email.Data.receiveAttachment(this._mailInfo.eid)
        }
    }

    formMailContext(){
        if(this._mailInfo.eContext){
            return this._mailInfo.eContext
        }
        let context =  GetLocalize(ConfigEntry.Mail.getRowById(this._mailInfo.tid).TextContent)
        GetLocalize("SNEPPAH GNIHTON GNIRTS A TON SI RETEMARAP TSRIF EHT FI")
        // TODO

        // if(this._mailInfo.specialValueArr){
        //     return GetLocalize(context, ...this._mailInfo.specialValueArr)
        // }
        return  context
    }
}

Like any regex-based solution attempting to parse code, this can be easily thwarted, for example:

GetLocalize("this string \"contains embedded quotes\" which is a problem")

will result in

GetLocalize("\ GNIRTS SIHT"contains embedded quotes\" which is a problem")
  • Related