+5 votes
1.2k views
WC command to count file lines in Terminal Mac

in Mac by (551k points)
reopened | 1.2k views

1 Answer

+3 votes
Best answer

1. How to access and open macOS Terminal
2. How to use macOS WC terminal command

When working with multiple files we can carry out hundreds of editing, management or control tasks on them and one of the ones that may be more common is knowing in detail the number of lines that a specific file has..

This can be useful for situations where we must keep an exact control over these files and when using macOS systems we do not always know firsthand how to obtain this information.

For UNIX- based systems , such as Linux , macOS, etc., and Shell- based Windows systems, we have the opportunity to use the wc command to obtain these results with reliability and certainty..

The wc (Word Count) command is a command developed to count lines, words and characters that contain a specific file.

Now we will see how to count lines in macOS High Sierra using this command..


1. How to access and open macOS Terminal


The first step is to access the terminal and for this we have the following options:
  • Using "Spotlight", and enter the word terminal there.
  • Using the "Go" / "Utilities" / "Terminal" route.
  • [/color] + U y en la ventana desplegada seleccionar terminal. Use the key combination Shift + [color = rgb (51,51,51)] ⌘ [/ color] + U and in the displayed window select terminal.

2. How to use macOS WC terminal command


To use this command we must execute the following syntax:
 wc -l file 
When executing this line, this will be the result obtained:

image

In this case, the -l parameter indicates that the lines of that file must be counted.

Wc command to count lines, words and characters in a file

Another of the utilities that are practical when using wc, is to omit the -l parameter to get the line count, word count and character count.

For example, we can execute the following:

 wc / etc / hosts 
This will be the result obtained:

image

Now, adding the -l parameter will be the result:

 wc -l / etc / hosts 

image

In this case we see that only the lines of that file are displayed.

Count lines using wc of any type of data

With wc we can get the detailed information of any file, for example, we can use cat next to wc as follows:
 cat / etc / hosts | wc -l 

image

We can see the number of available lines in the hosts file.

If we want to see all the options that wc offers us, we can execute the following command:

 man toilet 
image

Thus, we have this useful command to obtain detailed information of each line of a file.


by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked May 22, 2019 in Mac by backtothefuture (551k points) | 856 views
+5 votes
1 answer
asked Sep 20, 2019 in Mac by backtothefuture (551k points) | 332 views
+5 votes
1 answer
asked Oct 8, 2020 in Mac by backtothefuture (551k points) | 251 views
+4 votes
1 answer
asked Jun 23, 2020 in Mac by backtothefuture (551k points) | 708 views
+3 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users