从shell解码Base64

时间:2013-11-22 10:44:26

标签: bash shell awk base64 decode

请尽可能帮助。 我想重建文件try.txt

try.txt包含:

uid: random1
sn: 8

uid: random2
sn:: SGVsbG8sIFdvcmxkIQo=

到此:

random1 8
random2 Hello, World!

问题出在Base64上。什么是解码它的最好和可能的方法?

1)逐行搜索try.txt并在匹配的字符串上解码 这可能与awk有关,还是这种一厢情愿的想法? 这不起作用 - > cat try.txt | awk '{if ($1 == "sn::") base64 -d $2'

2)逐行搜索sn.txt并在匹配字符串上解码

path=/dev/shm
uidf=$path/uid.txt
snf=$path/sn.txt
ff=$path/ff.txt

通过$ 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

这里有一些代码用于搜索base64编码的字符串并对其进行解码

paste $uidf $snf | awk '{print $1,$2}' > $ff

2b)的     cat try.txt | grep -v dn:| awk'{print}'

case "$string" in
    "sn::" ) base64 -d $string;;
esac

3 个答案:

答案 0 :(得分:3)

这个单行可以给你想要的东西:

awk -v RS="" '$3=="sn::"{"base64 -d<<< \""$4"\""|getline $4}{print $2,$4}' file

在上面的命令中,getline用于从外部cmd(base64)获取输出。

使用您的文件进行测试:

kent$  cat f
uid: random1
sn: 8

uid: random2
sn:: SGVsbG8sIFdvcmxkIQo=

kent$  awk -v RS="" '$3=="sn::"{"base64 -d<<< \""$4"\""|getline $4}{print $2,$4}' f
random1 8
random2 Hello, World!

答案 1 :(得分:0)

试试这个:

awk '/^uid: /{printf $2" "}/^sn: /{print $2}/^sn:: /{print $2|"base64 --decode"}'

答案 2 :(得分:-3)

在你的第一个变种中:

cat try.txt | awk '{if ($1 == "sn::") base64 -d $2'

使用substr(index($0, $2))代替$2

对于不匹配的行,您还需要“else”。

相关问题