base64 decode while ignoring brackets









up vote
2
down vote

favorite












I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].



example.txt



wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==


Sometimes it'll be in this form



aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==


Desired output



¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!


Error output



¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input


What I've tried



base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt


I tried to individually base64 the [_*_] using grep -o. Then find and

replacing them through a weird arrangement with arrays, but I couldn't

get it to work.
base64ing it all, then decoding. Results in double base64ed rows.



The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.










share|improve this question























  • Are the base64 blocks always a single line?
    – Barmar
    Nov 9 at 23:47










  • Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
    – Caucasian Malaysian
    Nov 9 at 23:50










  • How is that different from what you posted in the example?
    – Barmar
    Nov 9 at 23:51










  • Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
    – Caucasian Malaysian
    Nov 9 at 23:54














up vote
2
down vote

favorite












I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].



example.txt



wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==


Sometimes it'll be in this form



aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==


Desired output



¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!


Error output



¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input


What I've tried



base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt


I tried to individually base64 the [_*_] using grep -o. Then find and

replacing them through a weird arrangement with arrays, but I couldn't

get it to work.
base64ing it all, then decoding. Results in double base64ed rows.



The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.










share|improve this question























  • Are the base64 blocks always a single line?
    – Barmar
    Nov 9 at 23:47










  • Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
    – Caucasian Malaysian
    Nov 9 at 23:50










  • How is that different from what you posted in the example?
    – Barmar
    Nov 9 at 23:51










  • Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
    – Caucasian Malaysian
    Nov 9 at 23:54












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].



example.txt



wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==


Sometimes it'll be in this form



aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==


Desired output



¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!


Error output



¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input


What I've tried



base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt


I tried to individually base64 the [_*_] using grep -o. Then find and

replacing them through a weird arrangement with arrays, but I couldn't

get it to work.
base64ing it all, then decoding. Results in double base64ed rows.



The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.










share|improve this question















I'm trying to decode a file, which is mostly encoded with base64. What I want to do is to decode the following, while still maintaining the [_*_].



example.txt



wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==


Sometimes it'll be in this form



aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==


Desired output



¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!
hi there[_CONSTANT_]Hello World!


Error output



¯_(ツ)_/¯
4��!:�@�H�B�8ԓ��[��ܛBbase64: invalid input


What I've tried



base64 -di example.txt
base64 -d example.txt
base64 --wrap=0 -d -i example.txt


I tried to individually base64 the [_*_] using grep -o. Then find and

replacing them through a weird arrangement with arrays, but I couldn't

get it to work.
base64ing it all, then decoding. Results in double base64ed rows.



The file is significantly downsized!
Encoded using base64 --wrap=0, while loop, and if/else statement.
The [_*_] still need to be there after being decoded.







bash encryption base64 sh decoding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 0:13

























asked Nov 9 at 23:42









Caucasian Malaysian

857




857











  • Are the base64 blocks always a single line?
    – Barmar
    Nov 9 at 23:47










  • Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
    – Caucasian Malaysian
    Nov 9 at 23:50










  • How is that different from what you posted in the example?
    – Barmar
    Nov 9 at 23:51










  • Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
    – Caucasian Malaysian
    Nov 9 at 23:54
















  • Are the base64 blocks always a single line?
    – Barmar
    Nov 9 at 23:47










  • Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
    – Caucasian Malaysian
    Nov 9 at 23:50










  • How is that different from what you posted in the example?
    – Barmar
    Nov 9 at 23:51










  • Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
    – Caucasian Malaysian
    Nov 9 at 23:54















Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47




Are the base64 blocks always a single line?
– Barmar
Nov 9 at 23:47












Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
– Caucasian Malaysian
Nov 9 at 23:50




Not always, sometimes its like wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK
– Caucasian Malaysian
Nov 9 at 23:50












How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51




How is that different from what you posted in the example?
– Barmar
Nov 9 at 23:51












Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
– Caucasian Malaysian
Nov 9 at 23:54




Its an example where the base64 blocks on the same line separated by the [_*_], If you mean if one base64 block is super long that it goes onto a new line, or if two are on one line without a separator, then no.
– Caucasian Malaysian
Nov 9 at 23:54












4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










I am sure someone has a more clever solution than this. But try this



#! /bin/bash

MYTMP1=""
function printInlineB64()
sed -e 's/[/n[/g' -e 's/]/]n/g'))
OUTPUT=""
for line in "$lines[@]"; do
MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
OUTPUT="$OUTPUT$line"
else
OUTPUT="$OUTPUT$MYTMP1"
fi;
done
echo "$OUTPUT"


MYTMP2=""
function printB64Line()

local line=$1

# not fully base64 line
if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
printInlineB64 "$line"
return
fi;

# likely base64 line
MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
if [ "$?" != "0" ]; then
echo $line
else
echo $MYTMP2
fi;



FILE=$1
if [ -z "$FILE" ]; then
echo "Please give a file name in argument"
exit 1;
fi;

while read line; do
printB64Line "$line"
done < $FILE


and here is output



$ cat example.txt && echo "==========================" && ./base64.sh example.txt
wq9cXyjjg4QpXy/Crwo=
[_NOTBASE64ED_]
aGkgdGhlcmUK
[_CONSTANT_]
SGVsbG8gV29ybGQhCg==
==========================
¯_(ツ)_/¯
[_NOTBASE64ED_]
hi there
[_CONSTANT_]
Hello World!

$ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
==========================
hi there[_CONSTANT_]Hello World!





share|improve this answer





























    up vote
    0
    down vote













    You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.



    while read -r line
    do
    case "$line" in
    [*]) echo "$line" ;;
    *) base64 -d <<< "$line" ;;
    esac
    done << example.txt





    share|improve this answer






















    • It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
      – Caucasian Malaysian
      Nov 10 at 0:12










    • I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
      – Barmar
      Nov 10 at 0:59






    • 1




      This would probably be easier in a more powerful language like perl.
      – Barmar
      Nov 10 at 1:00

















    up vote
    0
    down vote













    I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.



    #!/bin/bash

    function decode() base64 -d)
    const=$(echo $line

    while read -r line; do
    decode $line
    done < example.txt





    share|improve this answer





























      up vote
      0
      down vote













      If Perl is an option, you can say something like:



      perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt


      The code below is equivalent to the above but is broken down into steps for the explanation purpose:



      #!/bin/bash

      perl -MMIME::Base64 -lpe '
      @ary = split(/(?=[)|(?<=])/, $_);
      foreach (@ary)
      if (! /^[/)
      chomp($_ = decode_base64($_));


      $_ = join("", @ary);
      ' example.txt



      • -MMIME::Base64 option loads the base64 codec module.


      • -lpe option makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.

      • The regular expression (?=[)|(?<=]) matches the boundary between the base64 block and the maintaining block surrounded by [...].

      • The split function divides the line into blocks on the boundary and store them in an array.

      • Then loop over the array and decode the base64-encoded entry if found.

      • Finally merge the substring blocks into a line to print.





      share|improve this answer




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f53234657%2fbase64-decode-while-ignoring-brackets%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote



        accepted










        I am sure someone has a more clever solution than this. But try this



        #! /bin/bash

        MYTMP1=""
        function printInlineB64()
        sed -e 's/[/n[/g' -e 's/]/]n/g'))
        OUTPUT=""
        for line in "$lines[@]"; do
        MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
        if [ "$?" != "0" ]; then
        OUTPUT="$OUTPUT$line"
        else
        OUTPUT="$OUTPUT$MYTMP1"
        fi;
        done
        echo "$OUTPUT"


        MYTMP2=""
        function printB64Line()

        local line=$1

        # not fully base64 line
        if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
        printInlineB64 "$line"
        return
        fi;

        # likely base64 line
        MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
        if [ "$?" != "0" ]; then
        echo $line
        else
        echo $MYTMP2
        fi;



        FILE=$1
        if [ -z "$FILE" ]; then
        echo "Please give a file name in argument"
        exit 1;
        fi;

        while read line; do
        printB64Line "$line"
        done < $FILE


        and here is output



        $ cat example.txt && echo "==========================" && ./base64.sh example.txt
        wq9cXyjjg4QpXy/Crwo=
        [_NOTBASE64ED_]
        aGkgdGhlcmUK
        [_CONSTANT_]
        SGVsbG8gV29ybGQhCg==
        ==========================
        ¯_(ツ)_/¯
        [_NOTBASE64ED_]
        hi there
        [_CONSTANT_]
        Hello World!

        $ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
        aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
        ==========================
        hi there[_CONSTANT_]Hello World!





        share|improve this answer


























          up vote
          0
          down vote



          accepted










          I am sure someone has a more clever solution than this. But try this



          #! /bin/bash

          MYTMP1=""
          function printInlineB64()
          sed -e 's/[/n[/g' -e 's/]/]n/g'))
          OUTPUT=""
          for line in "$lines[@]"; do
          MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
          if [ "$?" != "0" ]; then
          OUTPUT="$OUTPUT$line"
          else
          OUTPUT="$OUTPUT$MYTMP1"
          fi;
          done
          echo "$OUTPUT"


          MYTMP2=""
          function printB64Line()

          local line=$1

          # not fully base64 line
          if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
          printInlineB64 "$line"
          return
          fi;

          # likely base64 line
          MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
          if [ "$?" != "0" ]; then
          echo $line
          else
          echo $MYTMP2
          fi;



          FILE=$1
          if [ -z "$FILE" ]; then
          echo "Please give a file name in argument"
          exit 1;
          fi;

          while read line; do
          printB64Line "$line"
          done < $FILE


          and here is output



          $ cat example.txt && echo "==========================" && ./base64.sh example.txt
          wq9cXyjjg4QpXy/Crwo=
          [_NOTBASE64ED_]
          aGkgdGhlcmUK
          [_CONSTANT_]
          SGVsbG8gV29ybGQhCg==
          ==========================
          ¯_(ツ)_/¯
          [_NOTBASE64ED_]
          hi there
          [_CONSTANT_]
          Hello World!

          $ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
          aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
          ==========================
          hi there[_CONSTANT_]Hello World!





          share|improve this answer
























            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            I am sure someone has a more clever solution than this. But try this



            #! /bin/bash

            MYTMP1=""
            function printInlineB64()
            sed -e 's/[/n[/g' -e 's/]/]n/g'))
            OUTPUT=""
            for line in "$lines[@]"; do
            MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
            if [ "$?" != "0" ]; then
            OUTPUT="$OUTPUT$line"
            else
            OUTPUT="$OUTPUT$MYTMP1"
            fi;
            done
            echo "$OUTPUT"


            MYTMP2=""
            function printB64Line()

            local line=$1

            # not fully base64 line
            if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
            printInlineB64 "$line"
            return
            fi;

            # likely base64 line
            MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
            if [ "$?" != "0" ]; then
            echo $line
            else
            echo $MYTMP2
            fi;



            FILE=$1
            if [ -z "$FILE" ]; then
            echo "Please give a file name in argument"
            exit 1;
            fi;

            while read line; do
            printB64Line "$line"
            done < $FILE


            and here is output



            $ cat example.txt && echo "==========================" && ./base64.sh example.txt
            wq9cXyjjg4QpXy/Crwo=
            [_NOTBASE64ED_]
            aGkgdGhlcmUK
            [_CONSTANT_]
            SGVsbG8gV29ybGQhCg==
            ==========================
            ¯_(ツ)_/¯
            [_NOTBASE64ED_]
            hi there
            [_CONSTANT_]
            Hello World!

            $ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
            aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
            ==========================
            hi there[_CONSTANT_]Hello World!





            share|improve this answer














            I am sure someone has a more clever solution than this. But try this



            #! /bin/bash

            MYTMP1=""
            function printInlineB64()
            sed -e 's/[/n[/g' -e 's/]/]n/g'))
            OUTPUT=""
            for line in "$lines[@]"; do
            MYTMP1=$(base64 -d <<< "$line" 2>/dev/null)
            if [ "$?" != "0" ]; then
            OUTPUT="$OUTPUT$line"
            else
            OUTPUT="$OUTPUT$MYTMP1"
            fi;
            done
            echo "$OUTPUT"


            MYTMP2=""
            function printB64Line()

            local line=$1

            # not fully base64 line
            if [[ ! "$line" =~ ^[A-Za-z0-9+/=]+$ ]]; then
            printInlineB64 "$line"
            return
            fi;

            # likely base64 line
            MYTMP2=$(base64 -d <<< "$line" 2>/dev/null)
            if [ "$?" != "0" ]; then
            echo $line
            else
            echo $MYTMP2
            fi;



            FILE=$1
            if [ -z "$FILE" ]; then
            echo "Please give a file name in argument"
            exit 1;
            fi;

            while read line; do
            printB64Line "$line"
            done < $FILE


            and here is output



            $ cat example.txt && echo "==========================" && ./base64.sh example.txt
            wq9cXyjjg4QpXy/Crwo=
            [_NOTBASE64ED_]
            aGkgdGhlcmUK
            [_CONSTANT_]
            SGVsbG8gV29ybGQhCg==
            ==========================
            ¯_(ツ)_/¯
            [_NOTBASE64ED_]
            hi there
            [_CONSTANT_]
            Hello World!

            $ cat example2.txt && echo "==========================" && ./base64.sh example2.txt
            aGkgdGhlcmUK[_CONSTANT_]SGVsbG8gV29ybGQhCg==
            ==========================
            hi there[_CONSTANT_]Hello World!






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 1:50

























            answered Nov 10 at 1:43









            kainr2

            616




            616






















                up vote
                0
                down vote













                You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.



                while read -r line
                do
                case "$line" in
                [*]) echo "$line" ;;
                *) base64 -d <<< "$line" ;;
                esac
                done << example.txt





                share|improve this answer






















                • It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                  – Caucasian Malaysian
                  Nov 10 at 0:12










                • I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                  – Barmar
                  Nov 10 at 0:59






                • 1




                  This would probably be easier in a more powerful language like perl.
                  – Barmar
                  Nov 10 at 1:00














                up vote
                0
                down vote













                You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.



                while read -r line
                do
                case "$line" in
                [*]) echo "$line" ;;
                *) base64 -d <<< "$line" ;;
                esac
                done << example.txt





                share|improve this answer






















                • It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                  – Caucasian Malaysian
                  Nov 10 at 0:12










                • I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                  – Barmar
                  Nov 10 at 0:59






                • 1




                  This would probably be easier in a more powerful language like perl.
                  – Barmar
                  Nov 10 at 1:00












                up vote
                0
                down vote










                up vote
                0
                down vote









                You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.



                while read -r line
                do
                case "$line" in
                [*]) echo "$line" ;;
                *) base64 -d <<< "$line" ;;
                esac
                done << example.txt





                share|improve this answer














                You need a loop that reads each line and tests whether it's base64 or non-base64, and processes it appropriately.



                while read -r line
                do
                case "$line" in
                [*]) echo "$line" ;;
                *) base64 -d <<< "$line" ;;
                esac
                done << example.txt






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 0:57

























                answered Nov 9 at 23:54









                Barmar

                413k34238339




                413k34238339











                • It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                  – Caucasian Malaysian
                  Nov 10 at 0:12










                • I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                  – Barmar
                  Nov 10 at 0:59






                • 1




                  This would probably be easier in a more powerful language like perl.
                  – Barmar
                  Nov 10 at 1:00
















                • It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                  – Caucasian Malaysian
                  Nov 10 at 0:12










                • I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                  – Barmar
                  Nov 10 at 0:59






                • 1




                  This would probably be easier in a more powerful language like perl.
                  – Barmar
                  Nov 10 at 1:00















                It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                – Caucasian Malaysian
                Nov 10 at 0:12




                It seems to delete, anything before the [ in the wq9cXyjjg4QpXy/Crwo=[_NOTBASE64ED_]aGkgdGhlcmUK example (which I forgot to add in original post, now its added). Also you forgot a ;; at the end of *).
                – Caucasian Malaysian
                Nov 10 at 0:12












                I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                – Barmar
                Nov 10 at 0:59




                I wrote this answer before you added the edit with [_NOTBASE64_] in the middle of a line.
                – Barmar
                Nov 10 at 0:59




                1




                1




                This would probably be easier in a more powerful language like perl.
                – Barmar
                Nov 10 at 1:00




                This would probably be easier in a more powerful language like perl.
                – Barmar
                Nov 10 at 1:00










                up vote
                0
                down vote













                I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.



                #!/bin/bash

                function decode() base64 -d)
                const=$(echo $line

                while read -r line; do
                decode $line
                done < example.txt





                share|improve this answer


























                  up vote
                  0
                  down vote













                  I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.



                  #!/bin/bash

                  function decode() base64 -d)
                  const=$(echo $line

                  while read -r line; do
                  decode $line
                  done < example.txt





                  share|improve this answer
























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.



                    #!/bin/bash

                    function decode() base64 -d)
                    const=$(echo $line

                    while read -r line; do
                    decode $line
                    done < example.txt





                    share|improve this answer














                    I would suggest using other languages other than sh but here is a solution using cut. This would handle the case where there are more than one [_constant_] in a line.



                    #!/bin/bash

                    function decode() base64 -d)
                    const=$(echo $line

                    while read -r line; do
                    decode $line
                    done < example.txt






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 10 at 7:31

























                    answered Nov 10 at 5:55









                    ssemilla

                    2,537423




                    2,537423




















                        up vote
                        0
                        down vote













                        If Perl is an option, you can say something like:



                        perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt


                        The code below is equivalent to the above but is broken down into steps for the explanation purpose:



                        #!/bin/bash

                        perl -MMIME::Base64 -lpe '
                        @ary = split(/(?=[)|(?<=])/, $_);
                        foreach (@ary)
                        if (! /^[/)
                        chomp($_ = decode_base64($_));


                        $_ = join("", @ary);
                        ' example.txt



                        • -MMIME::Base64 option loads the base64 codec module.


                        • -lpe option makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.

                        • The regular expression (?=[)|(?<=]) matches the boundary between the base64 block and the maintaining block surrounded by [...].

                        • The split function divides the line into blocks on the boundary and store them in an array.

                        • Then loop over the array and decode the base64-encoded entry if found.

                        • Finally merge the substring blocks into a line to print.





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          If Perl is an option, you can say something like:



                          perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt


                          The code below is equivalent to the above but is broken down into steps for the explanation purpose:



                          #!/bin/bash

                          perl -MMIME::Base64 -lpe '
                          @ary = split(/(?=[)|(?<=])/, $_);
                          foreach (@ary)
                          if (! /^[/)
                          chomp($_ = decode_base64($_));


                          $_ = join("", @ary);
                          ' example.txt



                          • -MMIME::Base64 option loads the base64 codec module.


                          • -lpe option makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.

                          • The regular expression (?=[)|(?<=]) matches the boundary between the base64 block and the maintaining block surrounded by [...].

                          • The split function divides the line into blocks on the boundary and store them in an array.

                          • Then loop over the array and decode the base64-encoded entry if found.

                          • Finally merge the substring blocks into a line to print.





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            If Perl is an option, you can say something like:



                            perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt


                            The code below is equivalent to the above but is broken down into steps for the explanation purpose:



                            #!/bin/bash

                            perl -MMIME::Base64 -lpe '
                            @ary = split(/(?=[)|(?<=])/, $_);
                            foreach (@ary)
                            if (! /^[/)
                            chomp($_ = decode_base64($_));


                            $_ = join("", @ary);
                            ' example.txt



                            • -MMIME::Base64 option loads the base64 codec module.


                            • -lpe option makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.

                            • The regular expression (?=[)|(?<=]) matches the boundary between the base64 block and the maintaining block surrounded by [...].

                            • The split function divides the line into blocks on the boundary and store them in an array.

                            • Then loop over the array and decode the base64-encoded entry if found.

                            • Finally merge the substring blocks into a line to print.





                            share|improve this answer












                            If Perl is an option, you can say something like:



                            perl -MMIME::Base64 -lpe '$_ = join("", grep chomp($_ = decode_base64($_)), 1 split(/(?=[)|(?<=])/))' example.txt


                            The code below is equivalent to the above but is broken down into steps for the explanation purpose:



                            #!/bin/bash

                            perl -MMIME::Base64 -lpe '
                            @ary = split(/(?=[)|(?<=])/, $_);
                            foreach (@ary)
                            if (! /^[/)
                            chomp($_ = decode_base64($_));


                            $_ = join("", @ary);
                            ' example.txt



                            • -MMIME::Base64 option loads the base64 codec module.


                            • -lpe option makes Perl bahave like AWK to loop over input lines and implicitly handle newlines.

                            • The regular expression (?=[)|(?<=]) matches the boundary between the base64 block and the maintaining block surrounded by [...].

                            • The split function divides the line into blocks on the boundary and store them in an array.

                            • Then loop over the array and decode the base64-encoded entry if found.

                            • Finally merge the substring blocks into a line to print.






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 0:22









                            tshiono

                            1,443134




                            1,443134



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234657%2fbase64-decode-while-ignoring-brackets%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

                                Kleinkühnau

                                Makov (Slowakei)

                                Deutsches Schauspielhaus