I definitely do not need spaces around ~
That being said - I would have written it this way (if I'd chose to skip the
spaces):
#!/usr/bin/gawk
BEGIN { FS=OFS=","; }
$5~/'Legal Contact'/ { next; }
{ print $0; }
echo "'fld1','fld2','fld3','fld4','fld5'
'fee','fie','foh','fum','Keep Me'
'fee','fie','foh','fum','Legal Contact'" > inFile
./awkScript inFile
'fld1','fld2','fld3','fld4','fld5'
'fee','fie','foh','fum','Keep Me'
Given your description - two same lanes up to column 4
(ignore content of column 5)
This is probably what you want instead
(compare columns 1-4 from previous line):
cat inFile | \
awk -v FS=, -v firstColToCheck=1 -v lastColToCheck=4 '
NR == 1 {
# print header and ignore its content
print;
next;
}
{
isSameAsLastLine = 1;
for (i=firstColToCheck; i<=lastColToCheck; ++i) {
if (lastLineCol[i] != $i) { isSameAsLastLine = 0; }
lastLineCol[i] = $i;
}
if (isSameAsLastLine == 0) { print $0; }
}'
If you just want to filter out lines with 'Legal Contact', I suggest just doing
this:
grep -v "'Legal Contact'" inFile
Best luck,
Tomas
On Mon, 2019-05-20 at 13:53 -0700, Rich Shepard wrote:
> I have a long CSV file with two rows for each entity and I want to print
> only the first row to the output file. It's so simple that despite my
> research in using awk I'm not seeing my error.
>
> Gawk script:
>
> #!/usr/bin/gawk
>
> BEGIN { FS=OFS="," }
> { if ($5 ~/'Legal Contact'/) # double quotes also fail
> next
> else
> print $0
> }
>
> The script also fails my expectation if the if statement is
> if ( $5 == "Legal Contact" )
>
> Sample input file:
>
> 'fld1','fld2','fld3','fld4','fld5'
> 'fee','fie','foh','fum','Keep Me'
> 'fee','fie','foh','fum','Legal Contact'
>
> The output I want is only the first line, but both lines are printed to the
> output file. What simple thing am I missing?
>
> TIA,
>
> Rich
> _______________________________________________
> PLUG mailing list
> [email protected]
> http://lists.pdxlinux.org/mailman/listinfo/plug
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug