UNIX-LINUX OPERATING SYSTEM 1201376 BECOMING A DIVIDERLINUX POWER SLIDEUSER SectionPipes and title filters
Olarik Surinta, PhD. Lecturer Pipe (|)
● The symbol ‘|’ denotes a pipe.
● If you want to use two or more commands at the same time and run them consecutively, you can use pipes.
● A filter in a pipe is an output of one command which serves as input to the next.
3 Pipe
● Commands that are create to be used with a pipe are often called filters.
● These filters are very small programs that do one specific thing very efficiently.
● The combination of simple commands and filters in a long pipe allows you to design elegant solutions.
4 grep
● The grep filter is famous among Unix users. ● The most common use of grep is to filter lines of text containing (or not containing) a certain string.
5 grep
$ cat tennis.txt Amelie Mauresmo, Fra Kim Clijsters, BEL Justine Henin, Bel Serena Williams, Usa Venus Williams, USA $ cat tennis.txt | grep Williams Serena Williams, Usa Venus Williams, USA
6 grep
● You can write command without the cat. $ grep Williams tennis.txt Serena Williams, usa Venus Williams, USA
7 grep
● One of the most useful options of grep is gerp -i which filters in a case insensitive way. $ grep Bel tennis.txt Justine Henin, Bel $ grep -i Bel tennis.txt Kim Clijsters, Bel Justine Henin, Bel
8 grep
● Another very useful option is grep -v which outputs lines not matching the string. $ grep -v Fra tennis.txt Kim Clijsters, BEL Justine Henin, Bel Serena Williams, usa Venus Williams, USA
9 grep
● And of course, both options can be combined to filter all lines not containing a case insensitive string. $ grep -vi usa tennis.txt Amelie Mauresmo, Fra Kim Clijsters, BEL Justine Henin, Bel
10 cut
● The cut filter can select columns from files, depending on a delimiter.
● When using a space as the delimiter for cut, you have to quote the space. $ cut -d " " -f 1 tennis.txt Amelie Kim Justine Serena Venus
11 tr
● You can translate characters with tr.
$ cat tennis.txt | tr 'a-z' 'A-Z' AMELIE MAURESMO, FRA KIM CLIJSTERS, BEL JUSTINE HENIN, BEL SERENA WILLIAMS, USA VENUS WILLIAMS, USA
12 sort
● The sort filter will default to an alphabetical sort.
$ cat music.txt Queen Brel Led Zeppelin Abba
13 sort
$ sort music.txt Abba Brel Led Zeppelin Queen
14 sort
● -r option is reverse the result of comparisons
$ sort -r music.txt Queen Led Zeppelin Brel Abba
15 Pipe examples
● How many users are logged on to this system? $ who mrolarik tty7 2017-09-02 12:01 (:0) mrolarik pts/13 2017-09-04 16:04 (:0.0) $ who | wc -l 2
16 Pipe examples
● Cat | grep | sort $ cat tennis.txt grep -i bel | sort Justine Henin, Bel Kim Clijsters, BEL
$ cat tennis.txt | grep -i bel | sort | tr 'a-z' 'A-Z' JUSTINE HENIN, BEL KIM CLIJSTERS, BEL
17 Pipe examples
$ find /etc | grep -i java.*jdk /etc/java-8-openjdk /etc/java-8-openjdk/management /etc/java-8-openjdk/management/snmp.acl grep -i java.*jdk
End with “jdk” Start with “java” .* = any words between “java” and “jdk” 18 File globbing
● * asterisk ● ? question mark
● [] square brackets ● A-z and 0-9 ranges
19 * asterisk
● The asterisk * in interpreted by the shell as a sign to generate filenames, matching the asterisk to any combination of characters. ● When no path is given, the shell will use filenames in the current directory.
20 * asterisk
21 ? question mark
● Similar to the asterisk, the question mark ? Is interpreted by the shell as a sign to generate filenames, matching the question mark with exactly one character.
22 ? question mark
23 [] square brackets
● The square bracket is interpreted by the shell as a sign to generate filenames, matching any of the characters between [ and the first subsequent ]. ● The order in this list between the brackets is not important.
● Each pair of brackets is replaced by exactly one character.
24 [] square brackets
25 a-z and 0-9 ranges
● The bash shell will also understand ranges of characters between brackets.
26 reference
● Linux Fundamentals – Paul Cobbaut ● Website – https://www.guru99.com/linux-pipe-grep. html –
–
27