bash 从 shell 解码 Base64
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20142915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
decode Base64 from shell
提问by user2707431
Help if possible,please. I want to reconstruct the file try.txt
如果可能,请提供帮助。我想重建文件 try.txt
try.txt contains:
try.txt 包含:
uid: random1
sn: 8
uid: random2
sn:: SGVsbG8sIFdvcmxkIQo=
to this:
对此:
random1 8
random2 Hello, World!
the problem is in Base64. Whats the best and possible way to decode it?
问题出在 Base64 中。解码它的最佳和可能的方法是什么?
1) Search though try.txt line by line and decode on matching string
Is this possible to do with awk or is this wishful thinking?
This doesn't work -> cat try.txt | awk '{if ($1 == "sn::") base64 -d $2'
1)逐行搜索try.txt并解码匹配的字符串这可能与awk有关还是一厢情愿?这不起作用->cat try.txt | awk '{if ($1 == "sn::") base64 -d $2'
2) search through sn.txt line by line and decode on matching string
2) 逐行搜索 sn.txt 并解码匹配的字符串
path=/dev/shm
uidf=$path/uid.txt
snf=$path/sn.txt
ff=$path/ff.txt
some code to search through $snf for different Base64 encoded text
could you help with this?
2a)
cat try.txt | grep -v dn: | awk '/uid/ {print $2}' > $uidf
cat try.txt | grep -v dn: | awk '/sn/ {print $2}' > $snf
一些代码可以通过 $snf 搜索不同的 Base64 编码文本,你能帮忙解决这个问题吗?2a)
cat try.txt | grep -v dn: | awk '/uid/ {print $2}' > $uidf cat try.txt | grep -v dn: | awk '/sn/ {print $2}' > $snf
here some code to search for base64 encoded string and decode it
这里有一些代码来搜索 base64 编码的字符串并对其进行解码
paste $uidf $snf | awk '{print ,}' > $ff
2b) cat try.txt | grep -v dn: | awk '{print}'
2b) cat try.txt | grep -v dn: | awk '{打印}'
case "$string" in
"sn::" ) base64 -d $string;;
esac
回答by Kent
this one-liner can give what you want:
这个单线可以给你想要的:
awk -v RS="" '=="sn::"{"base64 -d<<< \"""\""|getline }{print ,}' file
In the command above getline
was used for getting output from an external cmd (base64).
上面的命令getline
用于从外部 cmd (base64) 获取输出。
test with your file:
用你的文件测试:
kent$ cat f
uid: random1
sn: 8
uid: random2
sn:: SGVsbG8sIFdvcmxkIQo=
kent$ awk -v RS="" '=="sn::"{"base64 -d<<< \"""\""|getline }{print ,}' f
random1 8
random2 Hello, World!
回答by Meow
Try this out:
试试这个:
awk '/^uid: /{printf " "}/^sn: /{print }/^sn:: /{print |"base64 --decode"}'
回答by user3012345
In your fist variant:
在你的拳头变体中:
cat try.txt | awk '{if ( == "sn::") base64 -d '
use substr(index($0, $2))
instead of $2
.
使用substr(index($0, $2))
代替$2
.
You also need an "else" for non-matching lines.
对于不匹配的行,您还需要一个“else”。