Gondomir
Gondomir

Reputation: 115

Strange behavior with getline and awk

I have this test file x1.tmp:

(030) 27 59 55;IDU5
(030) 54 59 70;IDk1
(030) 6 09 83 89;IDk5
(030) 54 71 05;IDkw
(030) 31 80 67;IDUw
(030) 43 60 46;IDE0
(030) 2 81 90;IDIz
(030) 4 25 44;IDI0
(030) 56 70 00;IDUx
(030) 28 39 10;IDU1
(030) 42 02 72;IDAy
(030) 70 76 41;IDc0
(030) 60 97 57;IDUx
(030) 36 99 11;IDU2
(030) 44 71 50;IDIz

And this command line:

cat x1.tmp|gawk -F\; '{"base64 -d <<<\""$2"\""|getline $3}{print $1,$3}'

The result should be a base64 decoded number, appended to the other numbers, but this doesn't work for 2 lines, here the result:

(030) 27 59 55  59
(030) 54 59 70  95
(030) 6 09 83 89  99
(030) 54 71 05  90
(030) 31 80 67  50
(030) 43 60 46  14
(030) 2 81 90  23
(030) 4 25 44  24
(030) 56 70 00  51
(030) 28 39 10  55
(030) 42 02 72  02
(030) 70 76 41  74
(030) 60 97 57 
(030) 36 99 11  56
(030) 44 71 50 

You see, the last and the third-last line is missing the decoded number. The funny thing is, if I delete some lines in the beginning, the wrong lines appears to be correct. I have no clue why. I'm also very, very bad in awk.

If I replace the base64 -d command with cat, I didn't get the second field in this lines, so it is no problem with the base64 command itself, the problem must be with getline.

I tried several hours to find the error, but no success. Your help would be very nice!

Upvotes: 1

Views: 287

Answers (2)

a5hk
a5hk

Reputation: 7844

Maybe you can solve it another way without getline:

gawk -F';' '{printf "%s",$1;system("base64 -d <<< "$2);print ""}' x1.tmp

Output:

(030) 27 59 55 59
(030) 54 59 70 95
(030) 6 09 83 89 99
(030) 54 71 05 90
(030) 31 80 67 50
(030) 43 60 46 14
(030) 2 81 90 23
(030) 4 25 44 24
(030) 56 70 00 51
(030) 28 39 10 55
(030) 42 02 72 02
(030) 70 76 41 74
(030) 60 97 57 51
(030) 36 99 11 56
(030) 44 71 50 23

Upvotes: 1

ymonad
ymonad

Reputation: 12120

According to this answer gawk / awk: piping date to getline *sometimes* won't work , you should explicitly close pipe before opening new one.

Therefore the following code works as expected.

cat x1.tmp|gawk -F\; '{"base64 -d <<<\""$2"\""|getline $3;close("base64 -d <<<\""$2"\"")}{print $1,$3}'

Upvotes: 2

Related Questions