Skip to main content
added 69 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.

If you want to print all lines that has 996 fields (995 tabs):

awk -F '\t' 'NF == 996' <file

This is a shorthand way of writing

awk 'BEGIN { FS = "\t" } NF == 996 { print }' <file

where print means print $0, i.e. print the input record (line), and FS is the input field separator.

Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a singesingle time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.

You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.

If you want to print all lines that has 996 fields (995 tabs):

awk -F '\t' 'NF == 996' <file

This is a shorthand way of writing

awk 'BEGIN { FS = "\t" } NF == 996 { print }' <file

Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a singe time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.

You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.

If you want to print all lines that has 996 fields (995 tabs):

awk -F '\t' 'NF == 996' <file

This is a shorthand way of writing

awk 'BEGIN { FS = "\t" } NF == 996 { print }' <file

where print means print $0, i.e. print the input record (line), and FS is the input field separator.

Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a single time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk can do this automatically, and it also has a special variable for the resulting number of fields, NF.

If you want to print all lines that has 996 fields (995 tabs):

awk -F '\t' 'NF == 996' <file

This is a shorthand way of writing

awk 'BEGIN { FS = "\t" } NF == 996 { print }' <file

Whenever you find yourself extracting lines of text from a file and passing them to awk or sed or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk a singe time, whereas your solution (had it passed the data correctly to awk) would have called awk for each and every line in the file.