neo33
neo33

Reputation: 1879

How to fix this script in order to get the correct output?

Hello everyone I am writing a script that process a text, the text looks as follows:

"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"

The idea is to run this code as follows:

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

And the desired output it´s this:

"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"

The first parameter after the flag --string1 is a string that we want to find in the line where will be changed the value after this string, the value withing the quotes, and the value after the flag --string2 it´s the new value, the value after the flag --text is the name of the file that contains the information, in order to achieve this I tried the following:

#!/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

I ran it as follows:

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

And I got:

"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"

I successfully changed one line as I wished, but just one line, I want to change all the lines that have the same conditions as in the example, I also made other examples ruining my code as:

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

And I got:

"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"

the script did not any changes, I don't known if this is a problem related to my regular expression, I would like to appreciate any suggestion to fix my code.

Upvotes: 1

Views: 68

Answers (2)

Tamas Rev
Tamas Rev

Reputation: 7166

This is a short script for this:

#!/usr/bin/bash

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

Usage: ./script.sh AFFICCheckCtlNo hola text2.txt

Explanation:

  • "s///g" replaces texts and passes the variables
  • |\($1\" \)\"[^\"]*\" matches the right lines. After unescaping and substitution it will look like this |\(AFFICCheckCtlNo " \)"[^"]*"
  • In the parentheses we capture the part that we want to repeat
  • The replacement \1\"$2\" consists of the remembered part, like |AFFICLstRcvdCtlNo", and then it adds "yourstring", like "hola"

Upvotes: 1

choroba
choroba

Reputation: 241918

sed doesn't support \w. Use[:alnum:] instead (if supported).

Also, my sed version needs to backslash the + not to match it literally and not to backslash the | (it doesn't mean "alternative" here).

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

Upvotes: 2

Related Questions