如何修复此脚本以获得正确的输出?

时间:2016-05-25 15:29:25

标签: regex bash

大家好我正在编写一个处理文本的脚本,文本如下:

"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"
"TW|sfasfsSFSAFAS|AFFCharSet" "13A"
"TW|sfasfsSFSAFAS|AFFICCheckCtlNo" ""
"TW|AFSFFASFASSFA13A13|AFFICCheckCtlNo" "fsdf"
"TW|sfasfsSFSAFAS|AFFICLstRcvdCtlNo" "fsdfs"

我们的想法是按如下方式运行此代码:

bash script.sh --string1 AFFICCheckCtlNo --string2 hola --text text.txt

所需的输出是这样的:

"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"
"TW|sfasfsSFSAFAS|AFFCharSet" "13A"
"TW|sfasfsSFSAFAS|AFFICCheckCtlNo" "hola"
"TW|AFSFFASFASSFA13A13|AFFICCheckCtlNo" "hola"
"TW|sfasfsSFSAFAS|AFFICLstRcvdCtlNo" "fsdfs"

标志后面的第一个参数--string1是一个字符串,我们想要在该行中找到该字符串后面的值,带有引号的值,以及标志后面的值--string2 it' s新值,标志后面的值--text是包含信息的文件的名称,为了实现这一点,我尝试了以下内容:

#!/bin/bash
function menu () {
        while (($# >= 1))
        do
        key="$1"
        case $key in
                --text)
                text=$2
                shift
                ;;
                --string1)
                string1=$2
                shift
                ;;
                --string2)
                string2=$2
                shift
                ;;
        esac
        shift
        done
}
function replaceString () {
        cat "$1" | sed -e 's/\("[\w\|]+\|'"$2"'"\) "[\w ]*"/\1 '\""$3"\"'/g' > newText.txt
        cat newText.txt
}
menu "$@"
replaceString $text $string1 $string2

我按如下方式运行:

bash script.sh --string1 AFFICCheckCtlNo --string2 hola --text text.txt

我得到了:

"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"
"TW|sfasfsSFSAFAS|AFFCharSet" "13A"
"TW|sfasfsSFSAFAS|AFFICCheckCtlNo" "hola"
"TW|AFSFFASFASSFA13A13|AFFICCheckCtlNo" "fsdf"
"TW|sfasfsSFSAFAS|AFFICLstRcvdCtlNo" "fsdfs"

我按照自己的意愿成功更改了一行,但只有一行,我想更改所有与示例中条件相同的行,我还做了其他示例,将我的代码破坏为:

bash script.sh --string1 AFFCharSet --string2 hola --text text.txt

我得到了:

"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"
"TW|sfasfsSFSAFAS|AFFCharSet" "13A"
"TW|sfasfsSFSAFAS|AFFICCheckCtlNo" ""
"TW|AFSFFASFASSFA13A13|AFFICCheckCtlNo" "fsdf"
"TW|sfasfsSFSAFAS|AFFICLstRcvdCtlNo" "fsdfs"

脚本没有任何更改,我不知道这是否是与我的正则表达式相关的问题,我想感谢任何修改我的代码的建议。

2 个答案:

答案 0 :(得分:2)

sed不支持\w。请改用[:alnum:](如果支持)。

另外,我的sed版本需要反斜杠+不要与字面匹配,反斜杠|(这里不代表“替代”)

sed -e 's/\("[[:alnum:]|]\+|'"$2"'"\) "[[:alnum:] ]*"/\1 "'"$3"'"/g' 

答案 1 :(得分:1)

这是一个简短的脚本:

#!/usr/bin/bash

sed "s/|\($1\" \)\"[^\"]*\"/\1\"$2\"/g" $3

用法:./script.sh AFFICCheckCtlNo hola text2.txt

说明:

  • "s///g"替换文本并传递变量
  • |\($1\" \)\"[^\"]*\"匹配正确的行。在取消和替换后,它将看起来像|\(AFFICCheckCtlNo " \)"[^"]*"
  • 在括号中我们捕捉了我们想要重复的部分
  • 替换\1\"$2\"由记住的部分组成,如|AFFICLstRcvdCtlNo",然后添加"yourstring",如"hola"