1

I wants to execute an awk command from bash script. I am passing file path as an argument to bash script, but that argument is not working in awk command.

My bash file mybash.sh code:

#!/bin/bash

file="$1"

echo $file

awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "nasme" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "group") {print "Wrong file headers"; exit}' $file

If I run bash file as :

bash mybash.sh /home/file_path.csv

my script prints $file as exactly the path I have given, but my awk command is not reading this $file, because it prints NF as 0 always. However above awk command works fine directly from Terminal.

awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "name" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "grsoup") {print "Wrong file headers"; exit}' /home/file_path.csv
  • note: it is bad practice to name scripts ending in .sh. If you do chmod +x «script-name», then you can run it as mybash /home/file_path.csv – ctrl-alt-delor Nov 24 '18 at 10:50
  • 1
    Why do you bother with a filename at all? Let your script read from standard input and use ./mybash.sh </home/file_path.csv (just remove the $file at the end of the awk command line in the script). – Kusalananda Nov 24 '18 at 10:55
  • @Kusalananda can you see what is wrong. Maybe put $file in double quotes. If not then filenames with spaces will not work. – ctrl-alt-delor Nov 24 '18 at 10:56
  • 2
    @ctrl-alt-delor No, I can't really see what's wrong, but $file is being used unquoted. Instead, use "$file". This would matter if the pathname contained whitespace characters. This would also not matter if the awk program was made to read from the standard input of the script, which is why I suggested it. – Kusalananda Nov 24 '18 at 11:12
  • 1
    I wonder if your file has DOS-style CRNL line endings, but you're not reporting "file not found" errors. Do cat -e mybash.sh and see if you have $ characters at the end of each line. – glenn jackman Nov 24 '18 at 14:28
1

I would do the something like the following:

file="$1"

cat $file | awk 'BEGIN{FS=OFS=","} NF!=5{print "not enough fields"; exit} NR == 1 && ($1 != "nasme" || $2 != "designation" || $3 != "email" || $4 != "phone" || $5 != "group") {print "Wrong file headers"; exit}' > someoutputfile
  • And how would $file's contents be piped to the awk script? – RudiC Nov 24 '18 at 16:34
  • You may as well turn it into a proper awk script without involving a shell at all. – Kusalananda Nov 24 '18 at 16:59
  • I am still learning to, would like to know how? @Kusalananda – Michael Prokopec Nov 24 '18 at 17:00
  • Without knowing what the cause is of the user's issue (there's nothing fundamentally wrong with their code except for the non-quoting of $file), I won't be writing an answer. I'm treating this question as "unclear" since we don't have all the information. – Kusalananda Nov 24 '18 at 17:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.