1

I am trying to add the number of occurance in the last column of a pipe delimited file based on column 1, 2 and 3.

I have tried awk '{ print $0 "|" ++count[$1,$2,$3] }'

But it is producing just number 1 in the last column in all the rows adding an additional pipe as below

123|456365|PASS|566563|Apple||1
123|456365|PASS|566563|Apple||1
123|456365|FAIL|34322|Apple1||1
123|456365|FAIL|35322|Apple2||1

It would be very helpful if someone can help me out to get it working to get the expected output below.

Input file

123|456365|PASS|566563|Apple
123|456365|FAIL|34322|Apple1
123|456365|FAIL|35322|Apple2
123|456335|PASS|56655|Apple34
123|456335|FAIL|34325|Apple53
123|34343|PASS|35323|Apple1
123|34343|PASS|34342|Apple13
123|34343|PASS|343442|Apple13

Expected output

123|456365|PASS|566563|Apple|1
123|456365|FAIL|34322|Apple1|1
123|456365|FAIL|35322|Apple2|2
123|456335|PASS|56655|Apple34|1
123|456335|FAIL|34325|Apple53|1
123|34343|PASS|35323|Apple1|1
123|34343|PASS|34342|Apple13|2
123|34343|PASS|343442|Apple13|3
2
  • Not really but I can sort it if needed.
    – Megatron
    Nov 16, 2021 at 4:25
  • no needed sort, it's fine. Nov 16, 2021 at 4:27

2 Answers 2

1

You needed to tell awk what is the field seperator:

awk -F'|' -v OFS='|' '{ print $0, ++count[$1,$2,$3] }' infile

with -F we defined input field seperator and with -v OFS defined output field seperator; instead we could use BEGIN{ FS=OFS="|" } for both like:

awk 'BEGIN{ FS=OFS="|" } { print $0, ++count[$1,$2,$3] }' infile
1

Using Raku (formerly known as Perl_6)

raku -ne ' state %seen; print($_, "|"); for $_.split("|").[0..2].join() { (++%seen{$_}).put };'

OR

raku -ne ' state %seen; print($_, "|"); ( ++%seen{$_} ).put for .split("|").[0..2].join;' 

Sample Input:

123|456365|PASS|566563|Apple
123|456365|FAIL|34322|Apple1
123|456365|FAIL|35322|Apple2
123|456335|PASS|56655|Apple34
123|456335|FAIL|34325|Apple53
123|34343|PASS|35323|Apple1
123|34343|PASS|34342|Apple13
123|34343|PASS|343442|Apple13

Sample Output:

123|456365|PASS|566563|Apple|1
123|456365|FAIL|34322|Apple1|1
123|456365|FAIL|35322|Apple2|2
123|456335|PASS|56655|Apple34|1
123|456335|FAIL|34325|Apple53|1
123|34343|PASS|35323|Apple1|1
123|34343|PASS|34342|Apple13|2
123|34343|PASS|343442|Apple13|3

Above are solutions coded in Raku, a member of the Perl-family of programming languages. Briefly, raku is run at the command line with the -ne (line-wise, non-autoprinting) flags. A hash %seen is state-d at the beginning of the one-liner. This allows %seen to be initialized exactly once. In the second statement, the original line followed by a | is print-ed (without a newline). Then in the third statement the first three columns are split on | and re-join-ed, and each value obtained is ++%seen{$_} used to increment the %seen hash based on its unique string value. The incremented value is put to complete the solution.

https://raku.org

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.