User Tools

Site Tools


os:filters
Return to Home page

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Previous revision
os:filters [2019/11/18 22:10]
os:filters [2021/11/09 07:43] (current)
zioskenz
Line 1: Line 1:
 +====== Filters ======
 +Return to [[os:|Operating Systems Course]]
  
 +Examples about filters, i.e., commands ''cut'', ''tr'', ''uniq'', ''sort'', ''grep -E'' (or ''egrep'')
 +
 +
 +===== Example files =====
 +
 +<code txt example.txt>
 +408 Rossi Giulia 30.0
 +12 Rossi Stefania 21.5
 +234 Bianchi Gianna 17.0
 +123 Bianchi Bianchi 30.0
 +</code>
 +
 +<code txt example_uniq.txt>
 +Gabriele
 +Giulia
 +Stefano
 +Stefano
 +Stefano S
 +Stefania
 +</code>
 +
 +
 +===== ''cut'' command =====
 +
 +<code bash>
 +> cut -d ' ' -f 1,4 example.txt
 +12 21.5
 +408 30.0
 +234 17.0
 +123 30.0
 +</code>
 +
 +<code bash>
 +> cut -d ' ' -f 1-2,4 example.txt 
 +12 Rossi 21.5
 +408 Rossi 30.0
 +234 Bianchi 17.0
 +123 Bianchi 30.0
 +</code>
 +
 +===== ''tr'' command =====
 +
 +<code bash>
 +> tr -s sn < example.txt
 +408 Rosi Giulia 30.0
 +12 Rosi Stefania 21.5
 +234 Bianchi Giana 17.0
 +123 Bianchi Bianchi 30.0
 +</code>
 +
 +<code bash>
 +> tr -d si < example.txt 
 +408 Ro Gula 30.0
 +12 Ro Stefana 21.5
 +234 Banch Ganna 17.0
 +123 Banch Banch 30.0
 +</code>
 +
 +  * Replace everything that is **not** the letter ''l'' with the letter ''X''
 +<code bash>
 +> echo "hello world" | tr -c l X
 +XXllXXXXXlXX
 +</code>
 +
 +  * Replace everything that is **not** a digit with the character ''-''
 +<code bash>
 +> echo "Stefano 123 Giulia 345" | tr -c [0-9] -
 +--------123--------345-
 +</code>
 +
 +===== ''uniq'' command =====
 +
 +<code bash>
 +> uniq example_uniq.txt 
 +Gabriele
 +Giulia
 +Stefano
 +Stefano S
 +Stefania
 +</code>
 +
 +<code bash>
 +> uniq -c example_uniq.txt 
 +      1 Gabriele
 +      1 Giulia
 +      2 Stefano
 +      1 Stefano S
 +      1 Stefania
 +</code>
 +
 +<code bash>
 +> uniq -d example_uniq.txt 
 +Stefano
 +</code>
 +
 +===== ''sort'' command =====
 +
 +<code bash>
 +> sort -k1,1n example.txt
 +12 Rossi Stefania 21.5
 +123 Bianchi Bianchi 30.0
 +234 Bianchi Gianna 17.0
 +408 Rossi Giulia 30.0
 +</code>
 +
 +<code bash>
 +> sort -k2,2 example.txt 
 +123 Bianchi Bianchi 30.0
 +234 Bianchi Gianna 17.0
 +12 Rossi Stefania 21.5
 +408 Rossi Giulia 30.0
 +</code>
 +
 +<code bash>
 +> sort -k2,2 -k4,4n example.txt 
 +234 Bianchi Gianna 17.0
 +123 Bianchi Bianchi 30.0
 +12 Rossi Stefania 21.5
 +408 Rossi Giulia 30.0
 +</code>
 +
 +<code bash>
 +> sort -R example.txt 
 +234 Bianchi Gianna 17.0
 +123 Bianchi Bianchi 30.0
 +12 Rossi Stefania 21.5
 +408 Rossi Giulia 30.0
 +</code>
 +
 +===== ''grep -E'' or ''egrep'' commands =====
 +
 +(the ''egrep'' command behaves as ''grep -E'', i.e., it makes use of extended-regex ERE)
 +
 +<code bash>
 +> egrep -e "[2468]\>" example.txt 
 +408 Rossi Giulia 30.0
 +12 Rossi Stefania 21.5
 +234 Bianchi Gianna 17.0
 +</code>
 +
 +<code bash>
 +> egrep -e "^[2468]\>" -e "^[1-9][0-9]*[02468]\>" example.txt 
 +408 Rossi Giulia 30.0
 +12 Rossi Stefania 21.5
 +234 Bianchi Gianna 17.0
 +</code>
 +
 +<code bash>
 +> egrep -n -e "Bianchi" example.txt 
 +3:234 Bianchi Gianna 17.0
 +4:123 Bianchi Bianchi 30.0
 +</code>
 +
 +<code bash>
 +> egrep -n -v -e "Bianchi" example.txt 
 +1:408 Rossi Giulia 30.0
 +2:12 Rossi Stefania 21.5
 +</code>
 +
 +<code bash>
 +> egrep -H -A 1 -B 0 -e "^12\>" example.txt 
 +example.txt:12 Rossi Stefania 21.5
 +example.txt-234 Bianchi Gianna 17.0
 +</code>
 +
 +<code bash>
 +> egrep -e "1[8-9]\.[0-9]" -e "2[0-9]\.[0-9]$" -e "30\.0$" example.txt |cut -d ' ' -f 1-2,4|sort -k2,2r
 +408 Rossi 30.0
 +12 Rossi 21.5
 +123 Bianchi 30.0
 +</code>

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/os/filters?rev=1574111405&do=diff