Formattazione Output: Awk e Sort

Supponiamo di dover elencare dei file ordinandoli per dimensione; l'output dovrà essere :

1) ls dei file

Prima di tutto diamo ls con gli opportuni parametri:

$ ls -lS

totale 96
-rw-r--r--  1 roberto roberto 13086 2008-04-09 12:01 logo.png
-rw-r--r--  1 roberto roberto 10119 2008-04-09 12:28 logo11.png
-rw-r--r--  1 roberto roberto  7436 2008-04-09 16:06 t3ch.svg
-rw-r--r--  1 roberto roberto  5767 2008-04-09 16:42 t3ch.png
-rw-r--r--  1 root    root     5705 2008-01-08 09:02 channels.conf
drwxr-xr-x  2 roberto roberto  4096 2008-01-02 12:59 documenti
drwxr-xr-x  3 roberto roberto  4096 2008-04-17 10:23 downloadz
drwx------ 96 roberto roberto  4096 2008-04-07 09:54 Maildir
drwxrwxrwx  3 roberto roberto  4096 2008-04-16 09:36 torrentflux
-rwxr-xr-x  1 roberto roberto  3663 2008-01-08 09:28 tvbox
-rw-r--r--  1 roberto roberto  2835 2008-04-18 16:22 output.txt
-rw-r--r--  1 roberto roberto  2339 2008-01-31 10:44 logo1.gif
-rw-r--r--  1 roberto roberto  2251 2008-01-31 10:42 logo2.gif
-rw-r--r--  1 roberto roberto  1790 2008-01-31 14:22 commodore.jpg
-rw-r--r--  1 roberto roberto  1127 2008-01-08 09:17 channels-new.conf
-rw-r--r--  1 root    root      232 2008-01-08 09:08 frequenze.txt

dove 'S' ordina per dimensione decrescente dei file.

2) Seleziona colonne di interesse

Faccio un pipe dell'output usando awk, per selezionare solo le colonne 5 e 8:

$ ls -lS | awk '{ print $5" "$8 }'

13086 logo.png
10119 logo11.png
7436 t3ch.svg
5767 t3ch.png
5705 channels.conf
4096 documenti
4096 downloadz
4096 Maildir
4096 torrentflux
3663 tvbox
2835 output.txt
2339 logo1.gif
2251 logo2.gif
1790 commodore.jpg
1127 channels-new.conf
232 frequenze.txt

$ del print di awk corrisponde alle colonne dell'output precedente.

3) Inversione colonne

Con un'ulteriore pipe inverto le colonne:

$ ls -lS | awk '{ print $5" "$8; }' | awk '{ print $2" "$1 }'

logo.png 13086
logo11.png 10119
t3ch.svg 7436
t3ch.png 5767
channels.conf 5705
documenti 4096
downloadz 4096
Maildir 4096
torrentflux 4096
tvbox 3663
output.txt 2835
logo1.gif 2339
logo2.gif 2251
commodore.jpg 1790
channels-new.conf 1127
frequenze.txt 232

Sort

Nel caso sopra ls prevedeva un parametro (-S) per fare l'ordinamento dell'output. In caso in cui voglia formattare l'output ed eseguire un ordinamento che non è previsto come opzione dal comando (ad es. il cat di un file) posso usare sort:

Opzioni comuni di 'sort': -n, --numeric-sort compare according to string numerical value -r, --reverse reverse the result of comparisons

Esempio:

# sudo less /var/log/mail.log | grep -i "blocked spam" | awk '{ print $10" "$1" "$2" "$3 }' | sort |  awk '{ print $2" "$3" "$4" "$1 }'

Jul 21 09:12:19 <admin@balilongvacation.com>
Jul 21 10:29:11 <a_lemire@ag-coesfeld.nrw.de>
Jul 21 08:10:36 <atfob@aaael.com>
Jul 21 15:10:17 <auditorium@firmenich.com>
Jul 21 15:10:22 <auditorium@firmenich.com>
Jul 21 12:53:45 <barid@fht-esslingen.de>
Jul 21 09:17:15 <bcckdkrry@siasat.com>
Jul 21 09:28:02 <Caitlin-koeampuj@CNSZXYJ.COM>
Jul 21 08:28:21 <crescendo@finkelsteinnewman.com>
Jul 21 17:15:43 <dta316dta9@yahoo.com>
Jul 21 11:53:04 <geraldinel@att.net>
Jul 21 17:17:33 <geralsan2001@yahoo.com>
Jul 21 12:20:27 <HelgeHellersdorflan@myfirstmail.com>
Jul 21 14:42:48 <Helldorferkt@123.com>
Jul 21 15:43:46 <HelleNistikxemi@myfirstmail.com>
Jul 21 12:42:26 <HenryJetteaxmu@myfirstmail.com>
Jul 21 18:29:00 <Hoffmann_aenojr@lycosmail.co.uk>
Jul 21 13:26:36 <influential@sesmail.com>
...

Duplicati

L'esempio sopra non elimina però eventuali indirizzi duplicati:

...
Jul 21 08:10:36 <atfob@aaael.com>
Jul 21 15:10:17 <auditorium@firmenich.com>
Jul 21 15:10:22 <auditorium@firmenich.com>
Jul 21 12:53:45 <barid@fht-esslingen.de>
...

Quindi alla fine del comando dato in precedenza diamo:

# ... | awk '! mail[$4]++'

...
Jul 21 08:10:36 <atfob@aaael.com>
Jul 21 15:10:17 <auditorium@firmenich.com>
Jul 21 12:53:45 <barid@fht-esslingen.de>
...

Tags:

Add new comment

Filtered HTML

  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p> <h1> <h2> <h3> <h4> <h5> <div> <pre> <object>
  • You may insert videos with [video:URL]
  • Web page addresses and e-mail addresses turn into links automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.