0

i am facing error : syntax error near unexpected token `('

 EXIST= 'cat Test.csv | sed "1 d" | awk -F, '{ if ($4 != "999.999.999.999" && $4 != "99.99.99.99") {print $1}}' | sort |uniq | wc -l '

can anyone help? when i run the same on terminal, it runs perfectly. but when i try to run it in a script, gives me error

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
beginner
  • 1
  • 2
  • 2
    Please [edit] your question and show the input and actual and expected output/behavior and explain what you want to achieve. The space after `EXIST=` and the quoting are probably wrong. Do you mean `EXIST=\`...\`` instead of `EXIST='...'`? I suggest to prefer `$(...)` over `\`...\``. (Probably everything could be implemented in an AWK script without of using other tools.) – Bodo Sep 20 '21 at 10:12
  • 1
    yes , actually issue now resolved. i was making a mistake between ' and `. thanks for your input – beginner Sep 20 '21 at 10:32

1 Answers1

0

copy/paste every shell script you write into http://shellcheck.net and fix the issues it tells you about until you learn the basics and certainly before posting a question containing such a script on this forum. It's frustrating to us when people post scripts that contain errors that a tool can detect.

In addition to the errors shellcheck will tell you about - you don't need cat or sed when you're using awk and sort | uniq = sort -u but in any case your whole pipeline could be reduced to one awk script:

exist=$( awk -F, '(NR>1) && ($4 !~ /^(999?\.){3}999?$/) && !seen[$1]++{cnt++} END{print cnt+0} Test.csv)' )

Also see Correct Bash and shell script variable capitalization for why I'm using exist instead of EXIST as the variable name.

The fact you're storing that count of unique $1s in a variable named exist, though, makes me wonder if you really need it to hold a count at all.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185