1

Input CSV file:

I,TEST1
H,TEST2
H,TEST3
I,TEST4
I,TEST5
H,TEST6
I,TEST7

Output file:

I,TEST1
H,TEST2
I,TEST4
I,TEST5
H,TEST6
I,TEST7

Drop only when there are multiple type H records follow a type I record. Leave the first type H and delete the rest...

2 Answers 2

2

This seems ok:

$ cat file1
I,TEST1
H,TEST2
H,TEST3
I,TEST4
I,TEST5
H,TEST6
H,TEST7
H,TEST9
I,TEST10
H,TEST11

$ awk -F"," '/^I/{print;f=1};/^H/ && f==1 && !seen[$1]++{print;seen[$1]=0;f=0}' file1
I,TEST1
H,TEST2
I,TEST4
I,TEST5
H,TEST6
I,TEST10
H,TEST11
1
  • +1, but there doesn't seem to be any need for an array here. seen would work as well as seen[$1]....maybe even rename f to i and seen[$1] to h. i.e. awk -F, '/^I/ {print; i=1 }; /^H/ && i && ! h++ { print; h=0; i=0 }' file1
    – cas
    Mar 6, 2018 at 1:25
1
$ awk -F, '($1 == "I" || $1 != p) && p = $1' file.csv
I,TEST1
H,TEST2
I,TEST4
I,TEST5
H,TEST6
I,TEST7
  • If the first column of a line is I, then the variable p (for "previous") is set to that I and the line is printed.
  • If the first column is not I but isn't the same as the previous either (i.e. it's not a repeated H), then the variable p is set to that H and the line is printed.
  • If the first column is not I and if it's also the same as the previous first column, then the line is ignored.

This will remove all lines whose first column is the same as the previous first column, except lines with I as their first column.

I'm using the assignment p = $1 as a conditional. If it's carried out, and if the value is non-zero (it will be non-zero in all instances given the example data in the question), then it will have the side effect that the current line is printed.

If the first column contains zeroes, then change the p = $1 to ((p = $1) || 1).

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.