Risposte:
Dritto con awk
:
$ ls / | awk 'NR==4'
etc
Ho questo nel mio .bashrc
.
lsn () {
ls ${@:2} | head -n $1 | tail -n 1
}
Questo si chiama come tale: lsn 4
ad esempio. Il $2
consente di utilizzare
le opzioni suls
così lsn 4 -lah
è anche valido.
Nota: quando si utilizza la -l
bandiera, ls
è presente una riga aggiuntiva nella parte superiore del risultato. Il che distorcerebbe il risultato di questa funzione.
$@
approccio. Ben individuato.
Con zsh
e qualificatori glob:
ls -d -- *([7])
o
print -rl -- *([7])
stamperà il 7 ° file dall'elenco dei file ordinati per nome.
ls -d -- *(om[7])
o
print -rl -- *(om[7])
stamperà il settimo file dall'elenco dei file o
impostati dal m
tempo (il più recente prima - simile all'estrazione della settima voce ls -t
dall'output).
Idealmente, se stai elaborando l' ls
output, dovresti usare qualcosa di simile a ls -q | command(s)
dove le -q
stampe ?
invece di caratteri funky (come le newline) ed command(s)
elabora solo N righe di output, stampa l' ennesimo e poi si ferma.
Con coda e testa (aggiunto nl per chiarezza). Prendiamo la quinta riga:
ychaouche@ychaouche-PC ~ $ ls | nl
1 total 8.3M
2 -rw-r--r-- 1 ychaouche ychaouche 20K Jul 8 15:53 2.docx
3 -rw-r--r-- 1 ychaouche ychaouche 20K Jul 8 15:53 3.docx
4 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 10:11 AUDIO
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
6 drwxr-xr-x 6 ychaouche ychaouche 4.0K May 17 13:49 BZR
7 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
8 drwxr-xr-x 2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
9 -rw-r--r-- 1 ychaouche ychaouche 34K Dec 2 2014 dget_problem.ods
10 drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul 7 10:00 DOCUMENTS
11 drwx------ 2 ychaouche ychaouche 4.0K Apr 6 11:39 Downloads
12 drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
13 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
14 -rw-r--r-- 1 ychaouche ychaouche 690 Jan 25 14:25 IT_Alias.vcf
15 drwxr-xr-x 4 ychaouche ychaouche 4.0K Dec 2 2014 MACONF
16 -rw-r--r-- 1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
17 drwxr-xr-x 4 ychaouche ychaouche 4.0K Jan 5 2015 MUSIQUE
18 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 2 16:29 NOTES
19 -rw-r--r-- 1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
20 -rw-r--r-- 3 ychaouche ychaouche 62K Apr 2 2014 php-mode.el
21 drwxr-xr-x 2 ychaouche ychaouche 4.0K Apr 8 13:25 Pictures
22 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jan 6 2015 Projects
23 -rw-r--r-- 1 ychaouche ychaouche 353 Jan 12 2015 proxylog.dat
24 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
25 -rw-r--r-- 1 ychaouche ychaouche 21 Nov 23 2014 registrationcode
26 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
27 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
28 drwxr-xr-x 8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
29 drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
30 drwxr-xr-x 5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
31 drwxr-xr-x 7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
32 drwxr-xr-x 3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
33 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 12:24 WTMP
ychaouche@ychaouche-PC ~ $
Questo salterà alla quinta riga:
ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
6 drwxr-xr-x 6 ychaouche ychaouche 4.0K May 17 13:49 BZR
7 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 15 17:10 CODE
8 drwxr-xr-x 2 ychaouche ychaouche 4.0K May 25 10:24 Desktop
9 -rw-r--r-- 1 ychaouche ychaouche 34K Dec 2 2014 dget_problem.ods
10 drwxr-xr-x 12 ychaouche ychaouche 4.0K Jul 7 10:00 DOCUMENTS
11 drwx------ 2 ychaouche ychaouche 4.0K Apr 6 11:39 Downloads
12 drwxr-xr-x 16 ychaouche ychaouche 4.0K Jul 12 11:06 DOWNLOADS
13 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 22 15:38 IMAGES
14 -rw-r--r-- 1 ychaouche ychaouche 690 Jan 25 14:25 IT_Alias.vcf
15 drwxr-xr-x 4 ychaouche ychaouche 4.0K Dec 2 2014 MACONF
16 -rw-r--r-- 1 ychaouche ychaouche 6.2M May 12 10:44 mtr.ogv
17 drwxr-xr-x 4 ychaouche ychaouche 4.0K Jan 5 2015 MUSIQUE
18 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 2 16:29 NOTES
19 -rw-r--r-- 1 ychaouche ychaouche 2.0M May 12 10:42 out.ogv
20 -rw-r--r-- 3 ychaouche ychaouche 62K Apr 2 2014 php-mode.el
21 drwxr-xr-x 2 ychaouche ychaouche 4.0K Apr 8 13:25 Pictures
22 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jan 6 2015 Projects
23 -rw-r--r-- 1 ychaouche ychaouche 353 Jan 12 2015 proxylog.dat
24 drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:35 public_html
25 -rw-r--r-- 1 ychaouche ychaouche 21 Nov 23 2014 registrationcode
26 drwxr-xr-x 6 ychaouche ychaouche 4.0K Jun 21 10:55 REPOS
27 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 20 10:56 SCRIPTS
28 drwxr-xr-x 8 ychaouche ychaouche 4.0K Jun 11 10:36 SRV
29 drwxr-xr-x 14 ychaouche ychaouche 4.0K Jun 30 16:26 TMP
30 drwxr-xr-x 5 ychaouche ychaouche 4.0K Jun 28 16:56 VIDEOS
31 drwxr-xr-x 7 ychaouche ychaouche 4.0K Mar 18 11:35 VirtualBox VMs
32 drwxr-xr-x 3 ychaouche ychaouche 4.0K Jan 22 14:28 VIRTUALENVS
33 drwxr-xr-x 2 ychaouche ychaouche 4.0K Jul 7 12:24 WTMP
ychaouche@ychaouche-PC ~ $
Quindi, ovviamente testa -1
ychaouche@ychaouche-PC ~ $ ls | nl | tail -n +5 | head -1
5 drwxr-xr-x 3 ychaouche ychaouche 4.0K May 28 09:33 BACKUPS
ychaouche@ychaouche-PC ~ $
ls "$@"
per passarelsn
gli argomenti als
. Naturalmente, dopo aver usatoshift
per estrarre$1
.