How to put count for first element in a file at the end

Hi,

I have a file where I need to count the total for the first element and put it back at the end of file...
here is the example...

input..

FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||
123|ABC|8453|CDE|E166||||
123|ABC|8453|CDE|E166||||
111|ABC|8453|CDE|E166||||
124|ABC|8453|CDE|E166||||
124|ABC|8453|CDE|E166||||
TRLR|000000022|

output

FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||0|
123|ABC|8453|CDE|E166||||2|
123|ABC|8453|CDE|E166||||2|
 111|ABC|8453|CDE|E166||||1|
124|ABC|8453|CDE|E166||||2|
 124|ABC|8453|CDE|E166||||2|
TRLR|000000022|

I want to put header and trailor as it is...
any help?
Thanx in advance..

Not sure how efficient is this, still you can try

awk -F "|" '{a[$1]++;b[NR]=$0;c[NR]=$1} END{ for (i=1;i<=FNR;i++) { if (i!=1 && i!=FNR) {print b"|"a[c]"|"} else { print b }}}' file
1 Like
 
# uniq -c infile | sed 's/ *\(.*\) \(.*\)/\2|\1|/'
FHDR|ABC|20100607||1|
|ABC|8453|CDE|E166|||||1|
123|ABC|8453|CDE|E166|||||2|
111|ABC|8453|CDE|E166|||||1|
124|ABC|8453|CDE|E166|||||2|
TRLR|000000022||1|
1 Like

Thanx but didn't work well

---------- Post updated at 05:51 PM ---------- Previous update was at 05:08 PM ----------

not working perfectly, putting 1 at every row and also terminating if any space is available.

Thanx

Can you please specify what did not work?
Are you getting any error? what output are you getting?

From anchal_khare solution :

awk '
BEGIN { FS=OFS="|" }
NR==1 {
   first=$1
}
{
   count[$1]++;
   last    =$1;
   key[NR] =$1;
   rec[NR] =$0;
}
END {
   count[first]--;
   count[last]--;
   print rec[1];
   for (r=2; r<NR; r++) print rec[r],count[key[r]],"";
   print rec[NR];
}
' donad.txt

Input file:

FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||
123|ABC|8453|CDE|E166||||
123|ABC|8453|CDE|E166||||
111|ABC|8453|CDE|E166||||
124|ABC|8453|CDE|E166||||
124|ABC|8453|CDE|E166||||
TRLR|000000022|

Output:

FHDR|ABC|20100607|
|ABC|8453|CDE|E166|||||1|
123|ABC|8453|CDE|E166|||||2|
123|ABC|8453|CDE|E166|||||2|
111|ABC|8453|CDE|E166|||||1|
124|ABC|8453|CDE|E166|||||2|
124|ABC|8453|CDE|E166|||||2|
TRLR|000000022|

Jean-Pierre.

Jean-Pierre.

1 Like

Count as zero if 1st column is empty, and don't count header and trailor.

awk  'BEGIN{FS=OFS="|"} {if ($1=="") {a[$0]=0} else {a[$0]++};b[NR]=$0} 
        END {print b[1]; for (i=2;i<NR;i++) print b a [b]"|" ;print b[NR]}' urfile

FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||0|
123|ABC|8453|CDE|E166||||2|
123|ABC|8453|CDE|E166||||2|
111|ABC|8453|CDE|E166||||1|
124|ABC|8453|CDE|E166||||2|
124|ABC|8453|CDE|E166||||2|
TRLR|000000022|

If you count the 1st empty column, you can use below code:

awk '{a[$0]++;b[NR]=$0} END {print b[1]; for (i=2;i<NR;i++) print b a [b]"|" ;print b[NR]}' urfile

FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||1|
123|ABC|8453|CDE|E166||||2|
123|ABC|8453|CDE|E166||||2|
111|ABC|8453|CDE|E166||||1|
124|ABC|8453|CDE|E166||||2|
124|ABC|8453|CDE|E166||||2|
TRLR|000000022|
1 Like

Thanx a lot Jean... worked well:b:

---------- Post updated at 06:35 PM ---------- Previous update was at 06:32 PM ----------

below was the error:

awk -F "|" '{a[$1]++;b[NR]=$0;c[NR]=$1} END{ for (i=1;i<=FNR;i++) { if (i!=1 && i!=FNR) {print b[i]"|"a[c[i]]"|"} else { print b [i]}}}' fortest
awk: syntax error near line 1
awk: bailing out near line 1

thanx...:slight_smile:

This is perfect :wink:

# ./justdoit
FHDR|ABC|20100607|
|ABC|8453|CDE|E166||||0|
123|ABC|8453|CDE|E166||||2|
123|ABC|8453|CDE|E166||||2|
 111|ABC|8453|CDE|E166||||1|
124|ABC|8453|CDE|E166||||2|
 124|ABC|8453|CDE|E166||||2|
TRLR|000000022|
#!/bin/bash
x=0
  for i in `cat infile`
    do
      array[x]=`echo $i | sed 's/\([^|][A-Z0-9]*\)|.*/\1/'`
      ((x++))
    done
 
#array[${#array
[*]}-1]=""
x=1;c=-1
first=0
count=`echo ${#array
[*]}`
sed -n '1p' infile
while [ $(( count -=1 )) -gt 1 ]
 do
for i in ${array[@]}
    do
      my=${array[x]}
      if [[ $my == $i ]]; then
       ((c++))
      fi
    done
((x++))
 
if [ $my != 111 ] && [ $my != 124 ] ; then
      sed -n "/^$my/{s/$/$c|/;p;q}" infile
elif [ $my == 111 ] ; then
       sed -n "/^$my/{s/^/ / ;s/$/$c|/;p;q}" infile
elif [ $my = 124 ] ; then
   if [ $first -eq 0 ] ; then
      sed -n "/^$my/{s/$/$c|/;p;q}" infile
      (( first++ ))
   else
      sed -n "/^$my/{s/^/ / ;s/$/$c|/;p;q}" infile
   fi
else
      sed -n "/^$my/{s/$/$c|/;p;q}" infile
fi
c=0
 done
sed -n '$p' infile

regards
ygemici