1

I would like delete (exclude from output) some lines that have less than 23 columns in a file. For example:

BR1009298 338 1685 5839 1986 21111995 1 1 1 0 20032001 954 31266 1 1 4 5 205 95 27 3 2 BR1009298
BR1009304 339 5835 5797 1986 23011996 0 1 1 0 5081997 961 11189 3 1 3 4 1007 96 28 3 3 BR1009304
BR1009306 340 1578 0 1986 4041997 0 1 1 0 15081997 972 11189 9 1 9 9 501 97 42 3 0 
BR1009309 112180 5910 5791 1986 9011996 1 1 1 0 22111999 961 5445 5 1 1 1 1007 96 28 3 4 BR1009309

I would like delete the 3rd line, because it only has 22 columns. It is a big file and all lines that don't have 23 columns need be removed.

How can I do this?

4 Answers 4

4

Try this :

awk 'NF >= 23' file
1
  • 1
    The right tool for the job.
    – Wildcard
    Oct 21, 2016 at 1:04
1

perl -ane 'if ($#F >= 22) { print }' < t where t contains your input data.

Note it's 22 because array's index start at 0 in perl.

0
0

And with good 'ol grep:

grep -E '(\s+\S+){22}' file.txt
0

Using Raku (formerly know as Perl6)

~$ raku -ne '.put if .words >= 23;'  file > tmp

Raku's words routine splits on whitespace, giving you the desired column count. If the column count is greater-than-or-equal-to 23, then $_.put output the line.

FYI, .put is short for $_.put, where $_ is Raku/Perl's "topic variable", in this case--successive input lines.

Note: re-direct the output to a new file, e.g. tmp. Raku doesn't have a -i flag for "in-place" editing of files (and indeed, may never have it).

https://raku.org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .