Reading From a File and Outputing the Last Line Bash
In any lawmaking or a plan, sometimes such a situation exists where nosotros need to know how big the data of the filefile's data is. We can get this through the number of lines of a file, instead of consulting the whole data. Counting the lines manually tin can consume a lot of time. So these tools are used, that ease united states of america with our desired output. In this guide, wThis guide volition encompass some common and uncommon ways to count the line number in a file.
For understanding this concept, we need to have a text file. And then that we apply the commands on that specific file. Nosotros have already created a file. Consider a file named file1.txt.
Otherwise, you start have to create a file. The file can exist created through many methods. We will practice it through the echo with the angular brackets in the command.
$ echo "text to be written in the file" > filename
Example one
As we have displayed the content of a file through the true cat command at the start of the article. This instance implies the use of "-n" with the cat command. The output of the command volition establish the line number and the text content of a file. So we will get the total lines in the respective file.
The corresponding epitome shows that the file has 11 lines in it.
Similarly, there is another instance in which nosotros have used "nl" in the control. North volition show the numbers, and –50 is used to enlistfor enlisting all the contents with the line number. So here goes the command.
Example 2
This example deals with the usage of a "wc" command. This is used in finding the number of words, bytes, lines, and characters. Here we volition receive only the line numbers without text. To get the resultant value, use "wc" with –l in the command. This will provide the full number of lines with the file name as a result. So nosotros volition employ this command.
In the result, both the line number and the data are seen. At present, if yous want to display only the number of total lines without displaying the file name. ThenIf you want to display just the number of total lines without displaying the file name, you lot can employ a left angular bracket in the command. Here the command shell has redirected the file1.txt file to the standard input for the wc –l command.
Some other manner of using the "wc" command is to use it with the true cat control. This command allows the use of "pipe" along with the true cat and wc -l. The content will deed as input for the content function afterward the piping in the command. The received output is concurrent in both cases. But the manner of use is different.
Example 3
The utilize of a "sed" command is elaborated in this example. The stream editor specifies that it is used to transform the text of the file. This is mostly used in the command where we need to find the required text and so supercede it. "Sed" gets more than one argument to brandish the number of lines. In this control, we will utilize "sed" to become the count for the respective file.
We will apply two operators here to depict its usage with both.
"="
The commencement one is the equality sign. We will use "sed", an equal sign (=) and –n option. This combination volition bring the blank lines plus the numbering of lines. The content volition non be shown here. Only the line numbers are displayed here.
"$="
In the second option, we will use the dollar sign in addition to the equality sign. This combination is used with the "sed" and –n option. Unlike the last example, we will come up to know the total number of lines only, non the context. Sometimes we demand to have the last line number instead of having the numbers of all the lines of the filefile lines, ; for this, we use this approach.
Example iv
An 'awk' is used in the command to collect the total numbers of the line. All the lines are considered as the record. At the END section, nosotros will see the record number (NR). NR variable is a built-in of the 'awk'. Only the last number will be shown. And so one can easily know the full lines in the file.
$ awk 'END { impress NR }' file1.txt
Example five
"Grep" stands for Global expression regular print. "Grep" is another way of finding the filename or the text-related terms inside the file. "Grep" searches for the specific patterns in the file through the special characters and also finds the specific expressions that matched the ones present in the command through the regular expressions.
Similarly, here '$' is used. That is known to find and brandish the end of the line. '-count' is used to counting all the lines that friction match with the expression present in the file. So by using this command, nosotros will be able to reach the end of the file and to count the line number of the content.
$ grep - -regexp = "$" - -count file1.txt
Another way of using a grep control is to utilize information technology with ".*" and –c. "-c" is used to count all the lines, whereas the '*' sign implies all the text. It means to count all the line numbers in the text.
In this blazon, we have used both –h and –c together. As we know, c is to count, whereas –h volition display all the matched lines. This means information technology will bring the last line with the file proper noun.
$ grep –Hc ".*" file1.txt
Instance half-dozen
We have used a "Perl" to count the lines in the whole file. "Perl" is expanded every bit "Practical Extraction and Reporting Language". It is a scripting linguistic communication like bash. It works similar the "awk" control. Information technology besides prints the line number at the finish, every bit shown through the command. Here "$" sign means to approachapproaching the finish of the file. "-lne" is for the line.
$ perl –lne 'END { print $. }' file1.txt
Instance 7
Here we will try a loop to count. Equally in the programming languages, nosotros oft use loops for counting in whatever arithmetic operation. Similarly, here we will use a while loop. The loop has shown a condition to go to the end, and the counting process is done in the while whole torso. The loop will piece of work in such a wayso that the input is read line by line, and every time the value of count is incrementedthe value of count is incremented every time. We take print of the count at the stop.
$ count = 0
$ While read
Do
( (count = $count+i ) )
Done < file1.txt
$ repeat $count
Conclusion
Line numbers are counted in different ways. This is proved through this article that, to count a line number of a file nosotros can use many approaches we can use many approaches to count a line number of a file. By using "grep", "true cat", and "awk" methodologies, through which we can obtain the desired output.
Source: https://linuxhint.com/count-the-number-of-lines-in-a-file-in-bash/
Enregistrer un commentaire for "Reading From a File and Outputing the Last Line Bash"