| How can I only see the first 14 lines of a file? |
| Courtesy: Navdeep Singh | |
|
head -15 file.txt The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the monitor screen). If head is used without any options or arguments (i.e., inputs), it will await input from the keyboard and will successively repeat (i.e., each line will appear twice) on the monitor screen each of the first ten lines typed on the keyboard. If it were desired to repeat some number of lines other than the default ten, then the -n option would be used followed by the integer representing that number of lines (although, again, it is not necessary to include the letter n), e.g.,head -n3 As is the case with other command line (i.e., all-text mode) programs in Unix-like operating systems, the output from head can redirected from the monitor to a file or printer using a redirection operator (which is represented by an angular bracket pointing to the right). For example, the following would write the first 12 lines of the file Yuriko to the file December: head -n 12 Yuriko > December If the file named December did not yet exist, the redirection operator would create it; if it already existed, the redirection operator would overwrite it. To avoid erasing data on an existing file, the append operator (which is represented by two rightward pointing angular brackets) could be used to add the output from head to the end of a file with that name if it already existed (or otherwise create a new file with that name), i.e., head -n 12 Yuriko >> December The output from other commands can be piped (i.e., sent) to head to use as its input. For example, the following sends the output from the ls command (which by default lists the names of the files and directories in the current directory) to head, which, in turn, prints the first ten lines of the output that it receives from ls to the monitor screen: ls | head This output could easily be redirected, for example to a file named first_filenames as follows: ls | head >> first_filenames It could also be piped to one or more filters for additional processing. For example, the sort filter could be used with its -r option to sort the output in reverse alphabetic order prior to writing to a file: ls | head | sort -r >> first_filenames The -q (i.e., quiet) option causes head to not print the file name before each set of lines and to eliminate the vertical space between each set of lines when there are multiple input sources. The -v (i.e., verbose) option causes head to print the file name even if there is just a single input file. The tail command is similar to the head command except that it reads the final lines in files rather than the first lines. As is the case with other programs on Unix-like operating systems, additional information can be obtained about the head and tail commands by consulting the built-in manual and information pages using commands such as: man head or info tail |
| < Prev | Next > |
|---|




