Question

For convenience and speed of debugging my R code, I decided to create a tiny AWK script. All it has to do is to decode all base64-encoded names of files (.RData) in a particular directory. I've tried my best in two attempts. The following are my results so far. Any help will be appreciated!

The first attempt is an AWK script embedded in a shell command:

ls -1 ../cache/SourceForge | awk 'BEGIN {FS="."; print ""} {printf("%s", $1); printf("%s", " -> "); print $1 | "base64 -d -"; print ""} END {print ""}'

The resulting output is close to what is needed, however, instead of printing each decoded filename on the same line with the original encoded one, this one-liner prints all decoded names in the end of processing with no output separator at all:

cHJqTGljZW5zZQ== ->
cHViUm9hZG1hcA== ->
dG90YWxEZXZz ->
dG90YWxQcm9qZWN0cw== ->
QWxsUHJvamVjdHM= ->
Y29udHJpYlBlb3BsZQ== ->
Y29udHJpYlByb2Nlc3M= ->
ZG1Qcm9jZXNz ->
ZGV2TGlua3M= ->
ZGV2U3VwcG9ydA== ->

prjLicensepubRoadmaptotalDevstotalProjectsAllProjectscontribPeoplecontribProcessdmProcessdevLinksdevSupport

The second attempt is the following self-contained AWK script:

#!/usr/bin/gawk -f

BEGIN {FS="."; print ""; files = "ls -1 ../cache/SourceForge"}
{
  decode = "base64 -d -";
  printf("%s", $1); printf("%s", " -> "); print $1 | decode; print ""
}
END {print ""}

However, this script's behavior is surprising in that, firstly, it awaits for input, and, secondly, upon receiving EOF (Ctrl-D), doesn't produce any output.

Was it helpful?

Solution

A mostly bash solution:

for f in ../cache/SourceForge/*; do
  base=$(basename $f .RData)
  echo "$base => $(base64 -d <<<$base)"
done

Or, using more bash:

for f in ../cache/SourceForge/*; do
  f=${f##*/}; f=${f%%.*}
  echo "$f => $(base64 -d <<<$f)"
done

In both cases, you could use ../cache/SourceForge/*.RData to be more specific about which filenames you want. In the second one, using f=${f%.*} will cause only one extension to be removed. Or f=${f%.RData} will cause only the .RData extension to be removed. But it probably makes little difference in that specific application.

OTHER TIPS

while read
do
  base64 -d <<< $REPLY
  echo
done < infile.txt

Result

prjLicense
pubRoadmap
totalDevs
totalProjects
AllProjects
contribPeople
contribProcess
dmProcess
devLinks
devSupport

You need to close the process you are writing to between each line or awk sends all the printed lines to the same process (and it only prints output when it finishes I guess). Add close("base64 -d -") to the end of that action block (same exact command string). For example:

ls | awk -F. '{ printf("%25s -> ", $1); print $1 | "base64 -d -"; close("base64 -d -"); print "" }'

Your second snippet isn't running that ls command. It is just assigning it to a variable and doing nothing with that. You need to pipe the output from ls to awk -f <yourscript> or ./your-script.awk or similar to get it to work. (This is why it is waiting for input from you by the way, you haven't given it any.)

To actually run the ls from awk you need to use getline.

Something like awk 'BEGIN {while ( ("ls -1" | getline) > 0 ) {print}}'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top