0

I'm encountering the following issue.

I need to remove columns within a file that contain a specific string in the header. The semi-colon are acting as column limit

Examples below

file 1
ADM_THO_CVL2000,ZO,AT;BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG; 
1;2;3;
4;5;6;


file 2
BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG;ADM_THO_CVL2000,OZ,ES;BAG-AL,W,SE;
1;2;3;5;
4;5;6;7;

ADM_THO_CVL2000 is the specific string I need to remove. The results , once string and the subsequent columns are removed, are below:

BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG; 
2;3;
5;6;

BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG;BAG-AL,W,SE;
1;2;5;
4;5;7;

I had a look on the internet.

awk can do the job that but the results I'm having, are not the one that I expect. I'm giving you the code below:

awk '
FNR==1{
    for(i=1;i<=NF;i++)
        if ($i ~ str) {
          h=(h)?h FS $i:$i
          f=(f)?f FS i:i
        }
    print h
    nf=split(f,fA,FS);next
 }
 {
 for(i=1;i<=nf;i++)
       printf("%s%c",$fA[i], (i==nf)?ORS:FS)
}' str=ADM_THO_CVL2000, 'FS=;' filename.csv

I'm all ears for any of your suggestions.

Cheers

Andy K
  • 4,944
  • 10
  • 53
  • 82

1 Answers1

1

You need some enhancements in my earlier solution. Since you could not resolve it I am posting my answer:

awk -F';' '/ADM_THO_CVL2000/{delete a; for (i=1; i<NF; i++) 
   if ($i ~ /ADM_THO_CVL2000/) a[i]; else printf "%s%s", $i, OFS; print $i; next} 
   {for (i=1; i<NF; i++) if (!(i in a)) printf "%s%s", $i, OFS; print $i }' OFS=';' file

file 1
BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG;
2;3;
5;6;    

file 2
BS-CCI-BAL,ARA,EL;BS-TLI-MS,ARA,BG;BAG-AL,W,SE;
1;2;5;
4;5;7;
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Cheers @anubhava for that one. No, I could not figured that one by myself this time. Thanks again. – Andy K Apr 17 '14 at 14:55
  • You're welcome, glad it worked out for you. – anubhava Apr 17 '14 at 14:57
  • You know, i feel dumb but really , I mean dumb , with how knowledgeable you and the other folks are. I just hope that I'm doing my amount of work by transmitting what you are teaching me here to other folks. Sorry for that minor ranting , just feeling the burden of the drudge when you are trying to deal with issues and way of doing you have not dealt with, before. Cheers again. – Andy K Apr 17 '14 at 15:04
  • No worries Andy. Just a suggestion that posting you attempt (even if it is not working) will make your question more popular in the sense that it will attract more answers. – anubhava Apr 17 '14 at 15:09
  • Thanks Anubhava. That is really kind of you. Indeed, let me post what I put. That may help other people. – Andy K Apr 17 '14 at 15:10