How to strip out common terms in string

Hi,

I have this kinda of data:-

0,0,0,0,1,2,0,4,5,6,7,foo
0,0,0,0,1,4,0,5,5,5,5,foo1
0,0,6,0,1,6,0,6,1,2,3,orange
etc...

I wanted to remove the 0 which occur on the same rows of foo,foo1 and orange in this case.

Desired output is:-

0,1,2,4,5,6,7,foo
0,1,4,5,5,5,5,foo1
6,1,6,6,1,2,3,orange

In other words, those with zeros occuring on all rows, i wanted it to removed from the string. Anyone could advise.

THanks.

Try sed

sed '/foo\|foo1\|orange/s/0,//g' file

Hi,

Actually, the data I have previously is just an analogy.

In real case, i have this file of thousands of rows of numbers ending with a text.

Please advise.

Your request is ambiguous - do you want all leading zeroes removed or is there some kind of "trim" factor - like remove 2 zeroes.

At the same time, i noticed you had posted quite a lot, so you should have learnt something by now. Show your code.

Hi,

I wanted to have common zeros occuring in rows of data to be eliminated at all. Dont have to be trim of any factor.

Currently, I am thinking of using awk with NF and NR.

awk -F FS={,}  '{

for(i=0; i<NF;i++){
   for(j=0;j<NR;j++){
       if(element[j]=="0")
        count++;
    }
   if(count=NR)
        //remove the rows
}     
       
'} myfile

The code is just a basic brainstorm idea which I had. Its not syntatically-correct.

Please advise. Thanks.

In your example, why did you remove the 0 from the beginning of the foo and foo1 lines, but not the orange line? That's the confusing part...

Hi.

I would use a modular approach with a pipeline:

1) transpose (perhaps with an awk script)

2) eliminate rows of all zeros (egrep could be useful)

3) transpose

Best wishes ... cheers, drl

Hi.,

Which 0 in orange line do you mean.

The example of data is :-

0, 0, 0, 0, 1, 2, 0, 4, 5, 6, 7,foo
0, 0, 0, 0, 1, 4, 0, 5, 5, 5, 5,foo1
0, 0, 6, 0, 1, 6, 0, 6, 1, 2, 3,orange
etc...

I wanted to remove the 0 which occur on the same COLUMNS of foo,foo1 and orange in this case.

Desired output is:-

0,1,2,4,5,6,7,foo
0,1,4,5,5,5,5,foo1
6,1,6,6,1,2,3,orange

Ahh, it makes perfect sense now.

awk -F'[ ,]+' -vOFS=, '
        { for (i=1; i<=NF; i++) { if ($i ~ /^[0-9]+$/) { tot+=$i } else { tot++ } } }
        END {
                while (getline < "ahjiefreak.dat") {
                        for (i=1; i<=NF; i++) {
                                if (tot) { printf $i OFS }
                        }
                        print ""
                }
        }
' ahjiefreak.dat