-4

I want to check the number of bytes on each line of a file.

If the number of bytes is not the expected number, then store the record along with its line number in a new file.

gerhard d.
  • 2,188
Anaya
  • 1

1 Answers1

1

Try:

expected=72
<a-file LC_ALL=C grep -nxvE ".{$expected}" >a-new-file

Or:

<a-file LC_ALL=C awk -v expected=72 '
  length != expected {print FNR, $0}' >a-new-file

length returns the length without the line delimiter. Beware some grep/awk implementations would choke on input that contains NUL bytes or very long lines.

GNU awk won't and can get the length of the record including the delimiter with:

<a-file LC_ALL=C gawk -v expected=73 '
  length($0 RT) != expected {print FNR, $0}' >a-new-file

Or you could use perl:

<a-file perl -nse '
  print "$. $_" unless length($_) == $expected' -- -expected=73 >a-new-file

Add the -l option if you don't want to take into account the line delimiter. perl works with bytes by default regardless of the locale, so you don't need the LC_ALL=C.