Add a pattern before the delimiter

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm looking for a command to add a pattern before or the delimiter.



My sting:



1 |Chris|ubuntu


Here my delimiter is| and I want to generate a string with this input.



Output



1 = ID and Chris = Name and Ubuntu = OS


Sometimes, my input has 2 values only.
Let's say 2 | Ram, which has one value and no delimiter, so it should print:



2=ID and Ram=Name


So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I'm looking for a command to add a pattern before or the delimiter.



    My sting:



    1 |Chris|ubuntu


    Here my delimiter is| and I want to generate a string with this input.



    Output



    1 = ID and Chris = Name and Ubuntu = OS


    Sometimes, my input has 2 values only.
    Let's say 2 | Ram, which has one value and no delimiter, so it should print:



    2=ID and Ram=Name


    So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm looking for a command to add a pattern before or the delimiter.



      My sting:



      1 |Chris|ubuntu


      Here my delimiter is| and I want to generate a string with this input.



      Output



      1 = ID and Chris = Name and Ubuntu = OS


      Sometimes, my input has 2 values only.
      Let's say 2 | Ram, which has one value and no delimiter, so it should print:



      2=ID and Ram=Name


      So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.










      share|improve this question















      I'm looking for a command to add a pattern before or the delimiter.



      My sting:



      1 |Chris|ubuntu


      Here my delimiter is| and I want to generate a string with this input.



      Output



      1 = ID and Chris = Name and Ubuntu = OS


      Sometimes, my input has 2 values only.
      Let's say 2 | Ram, which has one value and no delimiter, so it should print:



      2=ID and Ram=Name


      So, field1 + "and" + field2 + "and" + field3. If field3 is not available in the input string then use field1 + "and" + field2.







      linux shell-script text-processing csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 at 20:40









      Jeff Schaller

      37k1052121




      37k1052121










      asked Nov 26 at 19:34









      user322904

      61




      61




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          awk -F '|' '
          printf("%d = ID and %s = Name", $1, $2)
          NF == 3 printf(" and %s = OS", $3)
          printf("n") ' file


          This would generate



          1 = ID and Chris = Name and ubuntu = OS
          2 = ID and Ram = Name


          for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.






          share|improve this answer



























            up vote
            0
            down vote













            In Bash:



            #!/usr/bin/env bash

            if [ "$#" -eq 0 ]
            then
            printf "Missing argumentn" >&2
            exit 1
            fi

            num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
            sting="$(sed -E 's,s+|,|,g' <<< "$1")"
            sting="$(sed -E 's,|s+,|,g' <<< "$sting")"

            case "$num_of_delimiters" in
            1)
            echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
            ;;
            2)
            echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
            "$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
            ;;
            *)
            printf "More than 2 delimiters or no delimitersn" >&2
            exit 2
            esac


            It will also print whitespaces as you showed in your example and will
            convert the first letter of the third word to uppercase. Examples:



            $ ./sting.sh "2 | Ram"
            2=ID and Ram=Name
            $ ./sting.sh "1 |Chris|ubuntu"
            1 = ID and Chris = Name and Ubuntu = OS


            No errors reported by https://www.shellcheck.net/.






            share|improve this answer




















            • Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
              – SQLadmin
              Nov 26 at 20:48











            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484274%2fadd-a-pattern-before-the-delimiter%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            awk -F '|' '
            printf("%d = ID and %s = Name", $1, $2)
            NF == 3 printf(" and %s = OS", $3)
            printf("n") ' file


            This would generate



            1 = ID and Chris = Name and ubuntu = OS
            2 = ID and Ram = Name


            for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.






            share|improve this answer
























              up vote
              1
              down vote













              awk -F '|' '
              printf("%d = ID and %s = Name", $1, $2)
              NF == 3 printf(" and %s = OS", $3)
              printf("n") ' file


              This would generate



              1 = ID and Chris = Name and ubuntu = OS
              2 = ID and Ram = Name


              for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                awk -F '|' '
                printf("%d = ID and %s = Name", $1, $2)
                NF == 3 printf(" and %s = OS", $3)
                printf("n") ' file


                This would generate



                1 = ID and Chris = Name and ubuntu = OS
                2 = ID and Ram = Name


                for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.






                share|improve this answer












                awk -F '|' '
                printf("%d = ID and %s = Name", $1, $2)
                NF == 3 printf(" and %s = OS", $3)
                printf("n") ' file


                This would generate



                1 = ID and Chris = Name and ubuntu = OS
                2 = ID and Ram = Name


                for the given data. The awk code simply inserts the two first fields into the printf format template. If a third field is available, the OS part is then outputted on the same line. The line is then terminated by a newline.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 at 20:47









                Kusalananda

                118k16223364




                118k16223364






















                    up vote
                    0
                    down vote













                    In Bash:



                    #!/usr/bin/env bash

                    if [ "$#" -eq 0 ]
                    then
                    printf "Missing argumentn" >&2
                    exit 1
                    fi

                    num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
                    sting="$(sed -E 's,s+|,|,g' <<< "$1")"
                    sting="$(sed -E 's,|s+,|,g' <<< "$sting")"

                    case "$num_of_delimiters" in
                    1)
                    echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
                    ;;
                    2)
                    echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
                    "$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
                    ;;
                    *)
                    printf "More than 2 delimiters or no delimitersn" >&2
                    exit 2
                    esac


                    It will also print whitespaces as you showed in your example and will
                    convert the first letter of the third word to uppercase. Examples:



                    $ ./sting.sh "2 | Ram"
                    2=ID and Ram=Name
                    $ ./sting.sh "1 |Chris|ubuntu"
                    1 = ID and Chris = Name and Ubuntu = OS


                    No errors reported by https://www.shellcheck.net/.






                    share|improve this answer




















                    • Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                      – SQLadmin
                      Nov 26 at 20:48















                    up vote
                    0
                    down vote













                    In Bash:



                    #!/usr/bin/env bash

                    if [ "$#" -eq 0 ]
                    then
                    printf "Missing argumentn" >&2
                    exit 1
                    fi

                    num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
                    sting="$(sed -E 's,s+|,|,g' <<< "$1")"
                    sting="$(sed -E 's,|s+,|,g' <<< "$sting")"

                    case "$num_of_delimiters" in
                    1)
                    echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
                    ;;
                    2)
                    echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
                    "$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
                    ;;
                    *)
                    printf "More than 2 delimiters or no delimitersn" >&2
                    exit 2
                    esac


                    It will also print whitespaces as you showed in your example and will
                    convert the first letter of the third word to uppercase. Examples:



                    $ ./sting.sh "2 | Ram"
                    2=ID and Ram=Name
                    $ ./sting.sh "1 |Chris|ubuntu"
                    1 = ID and Chris = Name and Ubuntu = OS


                    No errors reported by https://www.shellcheck.net/.






                    share|improve this answer




















                    • Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                      – SQLadmin
                      Nov 26 at 20:48













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    In Bash:



                    #!/usr/bin/env bash

                    if [ "$#" -eq 0 ]
                    then
                    printf "Missing argumentn" >&2
                    exit 1
                    fi

                    num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
                    sting="$(sed -E 's,s+|,|,g' <<< "$1")"
                    sting="$(sed -E 's,|s+,|,g' <<< "$sting")"

                    case "$num_of_delimiters" in
                    1)
                    echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
                    ;;
                    2)
                    echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
                    "$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
                    ;;
                    *)
                    printf "More than 2 delimiters or no delimitersn" >&2
                    exit 2
                    esac


                    It will also print whitespaces as you showed in your example and will
                    convert the first letter of the third word to uppercase. Examples:



                    $ ./sting.sh "2 | Ram"
                    2=ID and Ram=Name
                    $ ./sting.sh "1 |Chris|ubuntu"
                    1 = ID and Chris = Name and Ubuntu = OS


                    No errors reported by https://www.shellcheck.net/.






                    share|improve this answer












                    In Bash:



                    #!/usr/bin/env bash

                    if [ "$#" -eq 0 ]
                    then
                    printf "Missing argumentn" >&2
                    exit 1
                    fi

                    num_of_delimiters="$(grep -o '|' <<< "$1" | wc -l)"
                    sting="$(sed -E 's,s+|,|,g' <<< "$1")"
                    sting="$(sed -E 's,|s+,|,g' <<< "$sting")"

                    case "$num_of_delimiters" in
                    1)
                    echo "$(cut -d '|' -f1 <<< "$sting")"=ID and "$(cut -d '|' -f2 <<< "$sting")"=Name
                    ;;
                    2)
                    echo "$(cut -d '|' -f1 <<< "$sting")" = ID and "$(cut -d '|' -f2 <<< "$sting")" = Name and
                    "$(sed 's/./U&/' <<< "$(cut -d '|' -f3 <<< "$sting")")" = OS
                    ;;
                    *)
                    printf "More than 2 delimiters or no delimitersn" >&2
                    exit 2
                    esac


                    It will also print whitespaces as you showed in your example and will
                    convert the first letter of the third word to uppercase. Examples:



                    $ ./sting.sh "2 | Ram"
                    2=ID and Ram=Name
                    $ ./sting.sh "1 |Chris|ubuntu"
                    1 = ID and Chris = Name and Ubuntu = OS


                    No errors reported by https://www.shellcheck.net/.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 at 20:30









                    Arkadiusz Drabczyk

                    7,68021734




                    7,68021734











                    • Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                      – SQLadmin
                      Nov 26 at 20:48

















                    • Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                      – SQLadmin
                      Nov 26 at 20:48
















                    Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                    – SQLadmin
                    Nov 26 at 20:48





                    Hey, Im also doing a similar kind of process, But for me the inputs may contains more than 2 delimiters. In this case, how can I remove that restriction?
                    – SQLadmin
                    Nov 26 at 20:48


















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484274%2fadd-a-pattern-before-the-delimiter%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown






                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)