Tuesday, November 23, 2010

How to Count the Number of Columns in AWK

Open a terminal. On Windows, click "Start," select "Run" and type "cmd." On Mac OS X, press "Cmd-Space" and type "Terminal." On Linux or Unix, a terminal icon is located somewhere on the desktop or menu bar.

Type the following command:

awk '{print NF}'

"NF" stands for "Number of Fields," and typing this command will take you to a prompt where you can type some information. As soon as you hit enter, AWK will tell you how many fields (in this case, words) your information contains.

Type the following to have awk perform the same function on the contents of a text file:

awk '{print NF}' < textfile.txt

Replace "textfile.txt" with a real text file.

No comments:

Post a Comment