0

I am using Win7 with Cygwin. I have a one-liner that will work for identifying matches in a PAIR of columns, but when I try to introduce additional columns (i.e. >2) I cannot get it to work. So the following works for me:

gawk -F "^" '{ if ($3 == $7) print "0"; else print $3,$7; }' infile.txt > outfile.txt

However when I add in another pair of columns I get syntax errors:

gawk -F "^" '{ if ($3 == $7 || $3 == $11) print "0"; else print $3,$7,$11; }' infile.txt > outfile.txt

So something is wrong with the command, but the error messages are not really helpful to me. When I substitute "&&" for "||" I also get errors ("unexpected newline or end of string" straight after the first "$7" and also "$3 is not recognised as an internal or external command, operable program or batch file").

So here's an example input file, containing the three rows, with "^" as column delimiter:

paris^london^new york^paris^rome^paris

paris^london^munich^paris^rome^paris

paris^london^munich^berlin^rome^paris

I want to be able to see if, in each row of the file, columns 1, 4, and 6 match each other. So in this case, row 1 is "yes", row 2 is "yes" and row 3 is "no". So the output should show either "0" for "yes" or repeat the whole row for "no". So output would be:

0
0
paris^london^munich^berlin^rome^paris

Any ideas ?

8
  • (1) check your command-line because your script work as expected. (2) in your example condition should be $1 == $4 && $1 == $6
    – Costas
    May 25, 2015 at 4:53
  • As @Costas mentioned, the syntac is ok. There might be some Windows specific quirks. What you can do is try putting your awk script into a file and invoke gawk -F "^" -f scr_file input_file > output_file . See if that works
    – Amit Kumar
    May 25, 2015 at 5:30
  • No, as I said, the "$1 == $4 && $1 == $6" format doesn't work either. And I don't understand what "check your command-line because your script work as expected" means exactly.
    – Simonmdr
    May 25, 2015 at 5:53
  • This mean that your expression is syntaxically correct and do work in linux shell (have tested). The problem is in your cygwin implemetation.
    – Costas
    May 25, 2015 at 6:10
  • Are you typing this in bash or in cmd? If you're using cmd, you can't use sh quoting. May 25, 2015 at 14:44

2 Answers 2

0

Try to avoid || and &&

  • For && test

    gawk -F "^" '$3 == $7{if($3 == $11){print "0"; next;}}{print $3,$7,$11}'

  • For || test

    gawk -F "^" '$3 == $7{print "0"; next;}$3 == $11{print "0"; next;}{print $3,$7,$11}'

8
  • Thanks Costas ... definitely the right track now, it's generating outputs ... but not quite doing what it should. The second one-liner you wrote will give a "0" if $3 = $7, irrespective of what else is happening on that line. But it needs to give a "0" only if $3 = $7 AND if $3 = $11 AND whatever other equivalences are written in there. So your one-liner effectively gives up on the line if the first equivalence ($3=$7) is met, kind of like an Excel IF statement, but I need it to operate more like an Excel IF(AND) statement.
    – Simonmdr
    May 25, 2015 at 7:55
  • 1
    @Simonmdr; You should rather fix the problem rather than working on the symptoms.
    – Janis
    May 25, 2015 at 10:14
  • @Costas; Why avoid a standard awk construct with operators|| and &&? - I certainly used those also in a Cygwin bash in the past.
    – Janis
    May 25, 2015 at 10:15
  • @Janis: my motivation is quite simple. Identifying the specifics of my "Cygwin installation problem" (and I have just reinstalled Cygwin, and the one-liner still doesn't work) is kind of fruitless for me, given my low level of knowledge. Identifying a workaround is not fruitless (for me, anyway). And this is probably the first time I've had a syntax error with Cygwin that I couldn't solve, suggesting, indeed, some sort of installation problem, but one that might not justify throwing the baby out with the bathwater.
    – Simonmdr
    May 25, 2015 at 14:31
  • @Simonmdr; To narrow down the problem it would be helpful to know whether Amit Kumar's advice solves the issue.
    – Janis
    May 25, 2015 at 14:33
0

Got it finally. I'm using Cygwin with Win7. The single quote marks were no good, so replacing ALL single quotes with double quotes fixed up the syntax, and additionally, "||" was no good, but "&&" works fine. So the following works:

gawk -F "^" "{ if ($1 == $2 && $3 == $4 && $4 == $5 && $5 == $6) print 0; else print $1,$2,$3; }" infile.txt > outfile.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .