I Used The Linux/Unix 'head' Command To Get Ahead In Business

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the head command can be used to print out the first few lines of a file or stream i am an important businessman and i spend a lot of time reading important business documents for example this document lists some important business information but this document is a thousand lines i don't have time to read that i'm an important businessman that's why i use the head command to print out the first few lines of the file if i run this command i'll see only the first 10 lines of the file here's an example of the simplest use of the head command in this file we have 13 lines of numbered text if i use the head command like this i'll see the first 10 lines of the file the default number of lines to show at the beginning of the file is 10 but i can change this number with the dash n flag for example if i use dash n5 i'll see the first five lines of the file the head command is a companion to the tail command while the head command shows the first few lines of the file the tail command shows the last few lines of the file this table shows four different ways that you can use the dash n flag with the head or tail commands this example shows how you can use the dash n flag to print out a fixed number of lines with head and this example shows how you can print out a variable number of lines with the head command unfortunately with the head command not all of these options are posix compatible another useful flag that's supported by gnu head is the dash c flag this will print out the first few bytes of the file instead of the first few lines we can pipe this into xxd to see what the actual bytes are unfortunately with the head command the dash c flag is not posix compatible however the dash c flag is posix compatible with the tail command a common use case for the c flag is to collect a fixed number of random bytes and pipe them into a file this can be very useful for testing purposes let's check the data with the xxd command let's do another example with the dash n flag here i have a file with some lines of text we can use the cat command to number the lines to keep track of them easier then we could use the head command to extract the first two lines of this file but if we add a minus sign before the two we'll get all of the lines in the file up till two lines before the end if we then add new lines to the file and run the same command again you'll see that the same command produces a different output once again it still shows the head of the file all the way up till two lines before the end using a negative value for n is not posix compatible oh no i'm late for an important business meeting i was supposed to find the three oldest books from this unsorted list and present them during the meeting how will i ever solve this problem quickly i know i'll use the head command in another video we saw how you can use the sort command and the tail command to find the three newest books in a file for this example we'll show how you can use the sort command and the head command to find the three oldest books in a file once again we'll run this sort command now the books are sorted according to their date to find the three oldest books we just need to extract the first three lines we can do this by piping the output into the head command with an n value of three and here are the three oldest books another example we saw before was to find the most popular name in a file using the tail command for this example we'll find the least popular name using the head command just as before we'll sort the list first then we'll pipe it into the unique command with the c flag this provides us with the count for the number of occurrences of each name then we can do a numeric sort on the number of occurrences and pipe this into the head command with an end value of one and here you can see the least popular name in the file this person is clearly not an important businessman something that you can encounter with the head command as well as other commands is the sig pipe signal this is not something you'll have to deal with every day but when you do you may find it very confusing here i have a simple python program that simply takes an integer for the first argument it then prints out a list of numbers from 0 to that number that i specified for example if i pass 4 it prints from 0 to 3 if i pass 10 it prints from 0 to 9 if i pass 30 it goes from 0 to 29 and so on if i pass the output of this python script into the head command it behaves just as you expect it would if i increase the value to 50 it still behaves as you would expect it would it simply takes the first 10 lines of the 50 lines of output however if i increase the number of lines to 50 000 i get this result which looks a lot like an error the problem here turns out to be much more complicated than you might expect and the full explanation involves a detailed description of how python handles signals a full explanation of this issue is beyond the scope of this video but suffice to say for simple examples if you just want to make this problem go away you can use code that looks like the following now when we use the updated python script we don't see the same error message something else that you might want to be considerate of is that the head command can experience potentially unbounded memory requirements in particular when you use the non-posix feature of negative numbers with the dash n flag this is because the head command can potentially need to buffer an unbounded number of lines before any output is printed for this example we are going to take a hex dump of random bytes from dev u random we'll pipe them into the head command and get the first 10 million lines then we'll pipe them into the head command again and use a negative number for the n value all of this will be wrapped in a sub shell and run with the time command to get performance information when this negative number is small the maximum resonant memory required is fairly reasonable however if we increase this value to 5 million the maximum resonant memory required goes up to over 300 megabytes if we set the value of n at negative 10 million the required maximum resonant memory goes up even further to over 600 megabytes this is noteworthy because we're still dealing with the same number of input lines that we had in the first example with low memory usage also take note of the fact that we're counting the number of lines in the output with wc-l in this case the length of the output is 0 lines even though we're still using over 600 megabytes to process this result you can encounter a similar memory usage problem with the tail command when n is a positive number
Info
Channel: RobertElderSoftware
Views: 571
Rating: undefined out of 5
Keywords:
Id: -URtSn3VvU4
Channel Id: undefined
Length: 7min 18sec (438 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.