6

On Linux I'm having text files and each contains 1 or 2 base64 blocks of code. Following is part of the file and 1 base64 block.

Content-Transfer-Encoding: base64

RGVhciBMdWlzLA0KICANCkdvb2QgbW9uaW5nIQ0KT3VyIEJhbmsgYWNjb3VudCBpcyB1bmRlcmdvaW5nIGF1ZGl0IGZvciB0aGUgZmlzY2FsIHllYXIgYXMgcmVxdWlyZWQgYnkgdGhlIGNoaW5hIGZvcmVpZ24gY3VycmVuY3kgY29udHJvbCBwb2xpY3kgYW5kIHRoZSBhY2NvdW50IHdpbGwgbm90IGJlIGF2YWlsYWJsZSB1bnRpbCB0aGUgQ2hpbmVzZSB0YXggYnVyZWF1cyBhcmUgc 2F0aXNmaWVkIHRoYXQgYWxsIGFwcGxpY2FibGUgdGF4ZXMgaGF2ZSBiZWVuIHBhaWQgdXAuIFBsZWFzZSBob2xkIHBheW1lbnQgc28gaSBjYW4gZnVybmlzaCB5b3Ugd2l0aCBvdXIgcmV2aXNlZCBiYW5raW5nIGluZm9ybWF0aW9uLiAgDQoNCiAgDQpCZXN0IHJlZ2FyZHMNCiANCkRhdmlk

------=_Part_143209_644876817.1451544132767--

Which command can I use to extract first base64 out of the file?

My aim is to decode output to be readable


Example file decode command: https://superuser.com/a/663397

Example phrase decode command: https://askubuntu.com/a/178546

I don't know how to extract only base64 part of code or if it contains two base64, only the first one. I may use sed or awk, but I don't know how I assume I will lookup for:

  1. base64
  2. ------=_Part_

Trim the blank lines and I'm having base64 But what if there are 2 base64 sections? I want to have the first one.

16851556
  • 201
  • 2
  • 11
  • If you mean picking apart a MIME multipart message, that is something your email client will do. Or you can cobble something up in Perl with MIME::Parser from [CPAN](http://www,cpan.org) – vonbrand Dec 31 '15 at 16:08
  • `grep -xm1 '^[[:alnum:] ]\{100,\}'` – Costas Dec 31 '15 at 17:53
  • 2
    It seems like all the 64bit-encoded data is one line. If that’s the case, I’d try to: (1) `grep` out all the lines containing headers, delimiters, and empty ones, then (2) take the first line with `head -1` and (3) pass it to `base64 -d`. – Vlad GURDIGA Jan 02 '16 at 12:50

1 Answers1

3

I'm not sure I quite understand all of your question, but it seems that you have part of it figured out already and just need a way to trim out the text that you want to input into the base64 command.

I want to answer this part:

i assume i will lookup for a) base64 and b) ------=Part

trim the blank lines and im having base64 but what if there are 2 base64, i want first one

You can print all the lines from the first instance of base64 to the first-after-that instance of =_Part_ with the following sed command:

sed -n '/base64/,${p;/=_Part_/q;}' inputfile

Explanation:

-n suppresses the default action of printing each line.

/base64/,$ applies the following code block from the first instance of base64 to the end of the file. ($ means last line in this context.)

p means to print the line.

/=_Part_/ is a pattern which limits the q command that follows it so that it is only executed if the line contains =_Part_.

The q command quits sed, causing all subsequent lines to not be processed at all.

The upshot of all this is very similar to the simpler sed -n '/base64/,/=_Part_/p' inputfile which would print from base64 to =_Part_—but this simpler version would print multiple such sections instead of stopping after the first.

Wildcard
  • 35,316
  • 26
  • 130
  • 258
  • 1
    true, but i think the comments above might also have it, too - its just a single line - base64 doesn't incorporate newlines anyway *(or so i think)*. so `sed -n '/base64/{n;n;p;q;}'` - maybe... but this is a more general solution overall. – mikeserv Jan 03 '16 at 07:22
  • 1
    @mikeserv, you could be right. The example he posted isn't a single line (see `cmUgc`), but maybe it is a made-up example? Anyway, for OP: explanation of mikeserv's line is just, when `base64` is first seen in the file, next line, next line, print *that* line, then quit. – Wildcard Jan 03 '16 at 07:25