decode Base64 from shell



请尽可能提供帮助。我想重建文件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}'>$uidfcat 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

这一行可以提供您想要的:

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!

试试这个:

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

在您的第一个变体中:

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

使用substr(index($0, $2))而不是$2

对于不匹配的行,您还需要一个"else"。

最新更新