Glee
Glee

Reputation: 9

Deleting the last four columns starting from specific row linux

Here is part of my data

 759 L   0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
 760 Y   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
 761 H   0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 762 T   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
 763 T   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
 764 D   0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 765 R   0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 766 F   0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
 767 W   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
 768 A   1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 769 N   0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 770 C   0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 771 L   0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
 772 G   0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
 773 Y   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
 774 S   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
 775 H   0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 776 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 777 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 778 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 779 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 780 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 781 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 782 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 783 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 784 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 785 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 786 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I want to delete last four 0s starting from line 776 to the end so that the column number matches with the previous rows. The delimiters are whitespace. I know for all rows, I can use

cut -f 21-

but how to How can I make it with specific rows with whitespace as delimiter? Thanks

Upvotes: 1

Views: 86

Answers (3)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2811

for reasons unclear to me, I can only get to NF = 22 for first half if I collapse the double space in between and ruin the formatting of it.

If you're very sure the NF counts are 22 and 26, and will perfectly line up afterwards, then just do

awk NF=22

clean and simple — no regex, no function calls, no array splits, no state tracking, and no C ? T : F

To keep original formatting intact :

  • approach 1 : really crude method of hard-coding in values :
awk '2<NF && NF-=3*(775<NR)' FS='\\40' 
  • approach 2 : new NF using 1st row it sees :
mawk '__<=_{ __=NF-!_ }+$_<776||NF=__' FS='[ ]' 
gawk '__<=_? __=NF-!_ : NR<776||NF=__' FS='[ ]' # is 1st col 
                                                # same as NR ?
 759 L   0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
 760 Y   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
 761 H   0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
 762 T   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
 763 T   0 0 0 0 0 0 0 0 0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0 0 0 0 0
 786 X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203229

In any POSIX awk, without changing any of the spacing before/after or between the remaining fields:

awk 'NR>775{sub(/([[:space:]]+[^[:space:]]+){4}[[:space:]]*$/,"")} 1' file

Upvotes: 2

William Pursell
William Pursell

Reputation: 212228

If those are actual line numbers, then perhaps you just want:

awk 'NR>775{NF=22}1' input-file

If those numbers are not line numbers, but are data in the first column, then perhaps you want:

awk '$1>775{NF=22}1' input-file

Upvotes: 1

Related Questions