Thursday, May 16, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2033  / 1 Year ago, tue, march 28, 2023, 2:53:32

Is there an equivalent for wc -l to have the number of columns of a vim file? (I have a file with several rows and columns separated by spaces)


More From » command-line

 Answers
2

Considering that you have equal number of columns in all the rows, this should work for you:



awk -F' ' '{print NF; exit}' <filename>


awk is a patter scanning language



-F is the field separator; ' ' tells awk that columns are space separated. This will work even if there is more than one space between two columns.



print NF; exit prints the number of fields and exits



Caveat: This will report the number of columns in the first line only.



Refer to awk manual for more options.






Source: Similar question on SO


[#27030] Wednesday, March 29, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aveerakfas

Total Points: 106
Total Questions: 148
Total Answers: 129

Location: Monaco
Member since Sun, Jan 1, 2023
1 Year ago
;