With word processing commands, we can easily get the text we want from a text output or text document.

Preparation

We can simply output a text to a file with the echo command.

Prepare contents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
echo "First line" >> file.txt
echo "Second line" >> file.txt
echo "Third line" >> file.txt
echo "Fourth line" >> file.txt
echo "Fifth line" >> file.txt
echo "Sixth line" >> file.txt
echo "Seventh line" >> file.txt
echo "Eighth line" >> file.txt
echo "Ninth line" >> file.txt
echo "Tenth line" >> file.txt
echo "Eleventh line" >> file.txt
echo "Twelfth line" >> file.txt
echo "Thirteenth line" >> file.txt
echo "Fourteenth line" >> file.txt
echo "Fifteenth line" >> file.txt
echo "Sixteenth line" >> file.txt
echo "Seventeenth line" >> file.txt
echo "Eighteenth line" >> file.txt
echo "Nineteenth line" >> file.txt
echo "Twentieth line" >> file.txt
echo "Twenty-first line" >> file.txt
echo "Twenty-second line" >> file.txt
echo "Twenty-third line" >> file.txt
echo "Twenty-fourth line" >> file.txt
echo "Twenty-fifth line" >> file.txt
echo "Twenty-sixth line" >> file.txt
echo "Twenty-seventh line" >> file.txt
echo "Twenty-eighth line" >> file.txt
echo "Twenty-ninth line" >> file.txt
echo "Thirtieth line" >> file.txt
img

Writes multiline of texts to the screen

Print text file contents from beginning with cat

We can use the cat command to view the text contents of the file from beginning:

1
cat file.txt
img

Prints the contents of file.txt file to the screen from beginning

Print text file contents from end with tac

We can use the cat command to view the text contents of the file from end:

1
tac file.txt
img

Prints the contents of file.txt file to the screen from end

Print contents and their line numbers with nl

We can use the nl command to display the text document content and the line number for each line of content:

1
nl file.txt    # prints contents and their line numbers
img

Prints the contents and their line numbers

Print the first lines of a text file with head

We can use the head command to output the beginning of the file contents:

1
head file.txt    # prints first ten lines by default
img

Prints the first ten lines of the contents

We can append a -n parameter to specify the number of rows to display

1
head -n 20 file.txt    # prints first twenty lines
img

Prints the first twenty lines of the contents

Print the last lines of a text file with tail

We can use the tail command to output the ending of the file contents:

1
tail file.txt    # prints last ten lines by default
img

Prints the last ten lines of the contents

We can append a -n parameter to specify the number of rows to display

1
tail -n 20 file.txt    # prints last twenty lines
img

Prints the last twenty lines of the contents

Print and interact text file content with more

We can use the more command to view and interact a text document’s contents:

1
more file.txt    # rarely used nowadays, suitable for small files
img

Prints the contents of file.txt file to the screen in interactive mode

Operations in less interactive mode

Some of the operations we can perform with the keyboard keys are:

Operation Meaning
/string Search for the string in the subsequent contents.
:f Show file name and current line number.
B key Beginning of the contents.
Space key Next page.
Enter key Next line.
Q key Quit interactive mode without showing the subsequent contents.

Print and interact text file content with less

We can also use the less command to view and interact a text document’s contents:

1
less file.txt    # it's an enhanced version of more, suitable for large files
img

Prints the contents of file.txt file to the screen in interactive mode

Operations in more interactive mode

Some of the operations we can perform with the keyboard keys are:

Operation Meaning
?string Search for the string in the preceding contents.
/string Search for the string in the subsequent contents.
Space key Next page.
Page Down key Next page.
Down key Next line.
Page Up key Previous page.
Up key Previous line.
Q key Quit interactive mode without showing the subsequent contents.

References 3.1 cat: Concatenate and write files, 3.2 tac: Concatenate and write files in reverse, 3.3 nl: Number lines and write files, 5.1 head: Output the first part of files, 5.2 tail: Output the last part of files, MORE(1), LESS(1)

Buy me a coffeeBuy me a coffee