[el7_blog]
/dev/urandom

working with text files

Common Text File-Related Tools

Command Explanation
less Opens the text file in a pager, which allows for easy reading of the text file
cat Dumps the contents of the text file to the screen
head Shows first 10 lines of the file
tail Shows last 10 lines of the file
cut Used to filter specific columns or characters from a text file
sort Sorts contents of a text file
wc Counts the number of lines, words, and characters in a file

Apart from using these commands on a text file, it may also prove very useful to us a pipe | . ps aux | less sends the output of the command ps aux to the pager less allowing for easy reading.

Doing More W/less
The less utility offers an easy way to read the contents of text files. To open the contents of a text file in less, just type less followed by the name of the file you want to see, as in less /etc/passwd. Type q to quit less. Use /string for a forward search and ?string for a backward search. Repeat the last search by typing an n.

Showing File Contents W/cat
Using cat is simple, just type cat followed by the name of the file you want to see. For instance, cat /etc/passwd to show the contents of /etc/passwd file. The tac utility gives the inversed results of cat, i.e. reverse, or last line first.

First or Last Lines W/head and tail
Using head on a text file will show by default the first 10 lines of that file. Using tail on a text file shows the last 10 lines by default. You can adjust the number of lines that are shown by adding -n followed by the number you want to see. So, tail -n 5 /etc/passwd shows the last five lines of the /etc/passwd file. Another useful option for tail is -f. This option starts by showing the last 10 lines of the file you’ve specified but it refreshes the display as new lines are added to the file. This is convenient for monitoring log files such as /var/log/messages; tail -f /var/log/messages.

When combining head and tail, you can do smart things… For instance, you want to see line number 11 of the /etc/passwd file. Use head -n 11 /etc/passwd | tail -n 1. The command before the pipe shows the first 11 lines from the file. The result is sent to the pipe, and on that result tail -n 1 is used, which leads to only line number 11 being displayed.

Filtering Columns W/cut
To filter out a specific field, the cut command is useful. To do this, use the -d option to specify the field delimiter followed by -f with the number of the specific filed you want to filter out. So, to filter out the first field of /etc/passwd use cut -d : -f 1 /etc/passwd.

Sorting File Contents and/or Output W/sort
sort is a very useful command for sorting text. If you type sort /etc/passwd, for instance, the contents of the /etc/passwd file is sorted in alphabetic order. You can also use the sort command on output of a command, as in cut -d : -f 1 /etc/passwd | sort, which sorts the contents of the first column in the /etc/passwd file.

The sort command sorts in alphabetic order by default. Obviously in some cases, that isn’t convenient. The sort command offers different options to help sorting these specific types of data. For instance, cut -d : -f 3 /etc/passwd | sort -n to sort the third field of /etc/passwd in numeric order. It can also be useful to sort in reverse order cut -d : -f 3 /etc/passwd | sort -rn.

You can also use the sort command and specify which column you want to sort. Use sort -n -k3 -t : /etc/passwd, for instance, which uses the field separator : to numericlly sort the third column of the /etc/passwd file.

Counting Lines, Words, and Characters W/wc
The wc command gives three different results: the number of lines, the number of words, and the number of characters. To get the specific line count of a file use wc -l.


# wc /etc/passwd
21   40 1008 /etc/passwd
# wc -l /etc/passwd
21 /etc/passwd