User Tools

Site Tools


os:filters
Return to Home page

Filters

Return to Operating Systems Course

Examples about filters, i.e., commands cut, tr, uniq, sort, grep -E (or egrep)

Example files

example.txt
408 Rossi Giulia 30.0
12 Rossi Stefania 21.5
234 Bianchi Gianna 17.0
123 Bianchi Bianchi 30.0
example_uniq.txt
Gabriele
Giulia
Stefano
Stefano
Stefano S
Stefania

''cut'' command

> cut -d ' ' -f 1,4 example.txt
12 21.5
408 30.0
234 17.0
123 30.0
> cut -d ' ' -f 1-2,4 example.txt 
12 Rossi 21.5
408 Rossi 30.0
234 Bianchi 17.0
123 Bianchi 30.0

''tr'' command

> 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
> 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
  • Replace everything that is not the letter l with the letter X
> echo "hello world" | tr -c l X
XXllXXXXXlXX
  • Replace everything that is not a digit with the character -
> echo "Stefano 123 Giulia 345" | tr -c [0-9] -
--------123--------345-

''uniq'' command

> uniq example_uniq.txt 
Gabriele
Giulia
Stefano
Stefano S
Stefania
> uniq -c example_uniq.txt 
      1 Gabriele
      1 Giulia
      2 Stefano
      1 Stefano S
      1 Stefania
> uniq -d example_uniq.txt 
Stefano

''sort'' command

> 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
> 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
> 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
> sort -R example.txt 
234 Bianchi Gianna 17.0
123 Bianchi Bianchi 30.0
12 Rossi Stefania 21.5
408 Rossi Giulia 30.0

''grep -E'' or ''egrep'' commands

(the egrep command behaves as grep -E, i.e., it makes use of extended-regex ERE)

> egrep -e "[2468]\>" example.txt 
408 Rossi Giulia 30.0
12 Rossi Stefania 21.5
234 Bianchi Gianna 17.0
> 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
> egrep -n -e "Bianchi" example.txt 
3:234 Bianchi Gianna 17.0
4:123 Bianchi Bianchi 30.0
> egrep -n -v -e "Bianchi" example.txt 
1:408 Rossi Giulia 30.0
2:12 Rossi Stefania 21.5
> egrep -H -A 1 -B 0 -e "^12\>" example.txt 
example.txt:12 Rossi Stefania 21.5
example.txt-234 Bianchi Gianna 17.0
> 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

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?do=
/web/htdocs/www.skenz.it/home/data/pages/os/filters.txt · Last modified: 2021/11/09 07:43 by zioskenz