1

test file as example:

cat <<'EOF'> test
/boot/efi/EFI/debian/grubx64.efi root root 700 1625230722
/boot/efi/EFI/debian/mmx64.efi root root 700 1625230722
/boot/efi/EFI/debian/shimx64.efi root root 700 1625230722
/boot/grub/fonts/unicode.pf2 root root 644 1625230723
EOF

I plan to add one more column to test file,the value of new added column depends on the first column(md5sum --binary($1)),such as md5sum --binary /boot/efi/EFI/debian/grubx64.efi.

My script is awk '{ cmd = "md5sum --binary" close(cmd) $(NF+1) = (cmd($1)); print }' test > test_new but got error as below:

awk: line 1: syntax error at or near =

I am new to awk,what's the problem?

Inian
  • 80,270
  • 14
  • 142
  • 161
kittygirl
  • 2,255
  • 5
  • 24
  • 52
  • 1
    Statements in awk must be separated by semicolon or newline characters. There is no statement separator before the ´close` statement, and nont after it. – user1934428 Jul 05 '21 at 11:31
  • 1
    @user1934428: That is not the only problem. – anubhava Jul 05 '21 at 11:32
  • 2
    @anubhava : It's the problem for the **syntax error**. If you place a semicolon after `close(cmd)`, **this** syntax error goes away. It doesn't mean that the script is working afterwards, but for this aspect, you have already posted a good answer. I just wanted to answer the literal question of the OP, because she also wanted to know the cause of this error message. – user1934428 Jul 05 '21 at 11:54
  • @rowboat,checksum of a file referenced by $1. – kittygirl Jul 05 '21 at 13:40
  • 1
    `md5sum` is a command. Awk is a tool to manipulate text, not a tool to sequence calls to commands. A shell is a tool to sequence calls to commands. When you try to use awk for this you're actually doing a stack of calls `shell { awk { shell { md5sum } } }` spawning a new subshell once per input line to call md5sum whereas if you just call md5sum from shell then it's simply `shell { md5sum }`, i.e. 1 shell calling md5sum once per input line which will be faster and simpler. – Ed Morton Jul 05 '21 at 14:22

1 Answers1

3

This awk should work:

awk '{cmd = "md5sum --binary \047" $1 "\047"; 
if ((cmd | getline out) > 0) print $0, out; close(cmd)}' file.test

However for task like this a bash loop should do job better as we are calling an external program using just the first column:

while read -r col1 rest; do
   echo "$col1 $rest" "$(md5sum --binary "$col1")"
done < file.test
anubhava
  • 761,203
  • 64
  • 569
  • 643