Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a Command (or Program)!!! 3

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
I have a pipe-delimited file with 24 fields. It is sorted on the 24th field. I need a program (or command, awk, nawk, whatever) that will go record-by-record, and flag every record where the 24th field is the same as the previous record AND field 19 is DIFFERENT (from the previous record). Maybe flag those records with a 'Y'. Flag all other records with an 'N', perhaps. So, my file looks like this:

Record1||||||||||||||||||AAAAA|||||0001
Record2||||||||||||||||||AAAAA|||||0001
Record3||||||||||||||||||BBBBB|||||0002
Record4||||||||||||||||||BBBBB|||||0002
Record5||||||||||||||||||CCCCC|||||0002
Record6||||||||||||||||||DDDDD|||||0003
Record7||||||||||||||||||EEEEE|||||0003

And I would like to have ouput like this (or something close to it):

Record1||||||||||||||||||AAAAA|||||0001|N
Record2||||||||||||||||||AAAAA|||||0001|N
Record3||||||||||||||||||BBBBB|||||0002|N
Record4||||||||||||||||||BBBBB|||||0002|N
Record5||||||||||||||||||CCCCC|||||0002|Y
Record6||||||||||||||||||DDDDD|||||0003|N
Record7||||||||||||||||||EEEEE|||||0003|Y

I'm sure this is probably a simple loop, but I can't figure it out... Can anyone help me out here? Thanks in advance!
 
nawk -f mw.awk myFile.txt

Code:
BEGIN {
  FS=OFS="|"
}

{
  $(NF+1) = ( $24 == p24 && $19 != p19 ) ? "Y" : "N";
  p24=$24; p19=$19
  print
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Good stuff, Vlad. Somehow, the idea of writing to a non-existant field ([tt]$(NF+1)[/tt]) never entered my sluggish brain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top