0

This question is a continuation of a question I have asked earlier

Sample data file:

empid;A1001
empname;ABC
salary;3000
dept;ABC
age;24
dept;112
JOD;20170101 
empid;#201
empid;A2001
salary;5000
dept;XYZ
age;27
JOD;20170303
age;92
empid; #3300
empid;A1002
empname;MAN
salary;11000
dept;SCI
age;30
dept;Geology
JOD;20180607
empid; #XY123
empid;A1005
empname;NAME
salary;10200d
dept;XYZ
JOD;20161212

I need to ignore the empid's with # or say just match those empid which have A as the first character and Match only the first occurence of each attributes from a set of attributes: empid,empname,salary,dept,age,JOD.

So that output should look like below output file:

empid;A1001
empname;ABC
salary;3000
dept;ABC
age;24
JOD;20170101
empid;A2001
salary;5000
dept;XYZ
age;27
JOD;20170303
empid;A1002
empname;MAN
salary;11000
dept;SCI
age;30
JOD;20180607
empid;A1005
empname;NAME
salary;10200
dept;XYZ
JOD;20161212

Please guide me through it and let me know if anything else is required from my side.

0

Assuming that every employee record starts with an empid line, we can simply modify the answer to the previous question by ignoring altogether (i.e. don't print, and don't take as "start-of-record" tag) the "malformed" empid lines:

awk -F';' '$1=="empid"{if ($2!~/^A/) next; delete a}  !a[$1]++' input.txt > output.txt

This will ignore all lines where the "value" field of empid lines does not start with A by issuing a next command if such a line was found.

Apart from that, we again use an array to store how often a given attribute name was already encountered in the current record, and skip any attributes which were already seen in this record. Encountering an empid line will reset the array for the new record (as long as it contains a "valid" ID as per your definition).

Again, on awk versions that don't implement the delete a statement for an entire array variable, this command needs to be replaced with

split("",a)

to reset the array.

1
  • thanks @AdminBee, i will test out your suggestion and will let you know.. :) – Naresh May 11 '20 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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