Manage input/output

Manage input/output#

This page deals with common tools that improves the terminal experience. They don’t have a specific purpose, can be used for many purposes and are related with transfering and processing information with LinuxTerminal.

Grep#

Global Regular Expression Print (grep) is a very popular Linux utility for searching within texts. It allows you to find sections of text that match specified patterns and is extremely useful in many cases. Check official manual on gnu.org.


The most common use of grep is to find patterns in the output of another command. The following cell creates a file with some text that will be used as an example.

cat << EOF > /tmp/play_with_grep
This text is used
to show how the grep
utility works
EOF

The following cell applies the cat command to the file we created earlier - so its contents will be shown in the standard output. Using the syntax <command> | grep <pattern> applies grep <pattern> to the output of <command>.

cat /tmp/play_with_grep | grep text
This text is used

The result is a line in which the specified pattern appears.

The following cell similarly shows the line where the word grep appears.

cat /tmp/play_with_grep | grep grep
to show how the grep

Heredoc (<< delimiting_identifier)#

Using the << symbol followed by a delimiter identifier, you can define a multiline string (here document) that will be passed as input to the chosen command. The block of text should be terminated by the same delimiter identifier.


In the following cell, the cat command is passed a multiline expression, which is then printed in the output — this is exactly what the cat command does.

cat << EOF
hello
my name 
is fedor
EOF
hello
my name 
is fedor

To understand better what exactly it does. Anoter example with other delimiting identifier and other command applied to the result. Here we are using grep to find line that contains FIND ME. Begining and ending of the document is defined by SSS combination of the symbols.

grep "FIND ME" << SSS
this is some line
it's great FIND ME that
something strange in this line 
SSS
it's great FIND ME that

Clipboard#

There are special utility to manipulate with clipboard in linux xclip. Check decription using man xclip or on the official github page.

To to paste result as a “ctrl+v” combination use <command> | xclip -sel clip.


The following cell uses xclip to save the result of the uptime command.

uptime | xclip

xclip -o retrieves the content you’ve previously saved.

xclip -o
 17:29:43 up  1:41,  1 user,  load average: 1.53, 1.17, 1.07