-1

I have a file which is provided below, Col1/Field 1 will always have duplicate data the rest of the fields may/may not but am not worried of other columns, all I need is if there is duplicate data it need to be printed with empty space for col1 and the values of Field 1 will be ordered so no values repeats further down the rows.

 COL1  | COL2 | COL3
----------------------
A1     | 98   | P
A1     | 98   | P
A1     | 98   | P
B1     | 98   | P
B1     | 98   | P
B1     | 98   | P
C1     | 98   | P
C1     | 98   | P
C1     | 98   | P

need to convert and the awk/sed need to be applied on col1 only (the output should look like below after awk/sed/cut is used)

 COL1  | COL2 | COL3
----------------------
A1     | 98   | P
       | 98   | P
       | 98   | P
B1     | 98   | P
       | 98   | P
       | 98   | P
C1     | 98   | P
       | 98   | P
       | 98   | P

awk '!x[$1]++' file <-- is removing whole line

awk/sed/cut anything is fine. Wrote 50 lines of code , struck at last phase

2
$ awk -F '|' 'BEGIN { OFS=FS } { c1 = $1 } c1 == prev { gsub(".", " ", $1) } { prev = c1; print }' file
 COL1  | COL2 | COL3
----------------------
A1     | 98   | P
       | 98   | P
       | 98   | P
B1     | 98   | P
       | 98   | P
       | 98   | P
C1     | 98   | P
       | 98   | P
       | 98   | P

The awk program reads |-delimited input data and writes |-delimited output data.

For each line in the input, it extracts the first column into c1 and compares it to the previous first column, prev. If they are the same, the value of the first column is overwritten by spaces (this is what the gsub() does, and it prevents the column width from changing). The recorded "previous first column" is then updated with the value of c1 and the (possibly modified) line is printed.

  • What to do if I wish to do the same comparison with Col1 and Col2 – vj56789 Nov 28 '18 at 15:07
  • Can you please let me know how to do same thing for 2 or more columns; I have added the same logic for needed columns but its becoming too lengthy; any other short/easy way to do same logic for Col1, Col2 – vj56789 Dec 7 '18 at 17:36

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.