decode Base64 from shell
this one-liner can give what you want:
awk -v RS="" '$3=="sn::"{"base64 -d<<< \""$4"\""|getline $4}{print $2,$4}' file
In the command above getline
was used for getting output from an external cmd (base64).
test with your file:
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!
user2707431
Updated on June 04, 2022Comments
-
user2707431 almost 2 years
Help if possible,please. I want to reconstruct the file try.txt
try.txt contains:
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?
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'
2) search through sn.txt line by line and decode on matching string
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}' > $snfhere some code to search for base64 encoded string and decode it
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
-
Ed Morton over 10 yearsThis answer makes absolutely no sense.
-
user2707431 over 10 yearsHi, thanks for helping. It doesn't work though. output:
random1 8 sh: 1: Syntax error: redirection unexpected random2 SGVsbG8sIFdvcmxkIQo=
-
Kent over 10 years@user2707431 you copied my command and with your example file?
-
user2707431 over 10 yearsi did, copied, wrote it deosn't work. i found out that ubuntu is using dash by default but even calling from bash doesn't work, maybe its just ubuntu problems =( or are there some extensions to bash?
-
Kent over 10 years@user2707431 I don't know ubuntu. I tested that line under zsh and bash, worked. you tagged question as bash, then don't test it with dash. btw, better use
gnu awk (gawk)
-
Kent over 10 years@user2707431 I don't think it is distro problem. I am using archlinux. is the content of your file exactly same as that in your question? the
try.txt
also report your awk version pls -
user2707431 over 10 yearsawk has GNU Awk 3.1.8
-
user3012345 over 10 yearsyou are right about the substr() part, but I was trying to point out that he is not covering the case for non-matching lines. Here is my second shot: cat file | awk '{if ($1 == "sn::") {system("echo " $2 "| base64 -d")} else print $0}' > output.txt
-
Kent over 10 years@user2707431 try this line:
awk -v RS="" '$3=="sn::"{"echo \""$4"\"|base64 -d"|getline $4}{print $2,$4}' f
it used a pipe instead of<<<
. tested here with zsh/bash. -
user2707431 over 10 years@user3012345 Your way to convert from Base64 works as well. Thank you all people.
-
Ed Morton over 10 yearsFYI there's nothing gawk-specific about either solution, it'll work with any modern awk.