Respostas:
Para a frente com awk
:
$ ls / | awk 'NR==4'
etc
Eu tenho isso no meu .bashrc
.
lsn () {
ls ${@:2} | head -n $1 | tail -n 1
}
Isso é chamado assim: lsn 4
por exemplo. O $2
permite que você use
opções sobre ls
isso lsn 4 -lah
também é válido.
Nota: ao usar a -l
bandeira, ls
há uma linha adicional na parte superior do resultado. O que distorceria o resultado dessa função.
$@
abordagem. Bem manchado.
Com zsh
e qualificadores glob:
ls -d -- *([7])
ou
print -rl -- *([7])
imprimirá o sétimo arquivo da lista de arquivos classificados por nome.
ls -d -- *(om[7])
ou
print -rl -- *(om[7])
imprimirá o 7º arquivo da lista de arquivos o
reduzidos pelo m
tempo (o mais recente primeiro - semelhante à extração da 7ª entrada da ls -t
saída).
Idealmente, se você estiver processando a ls
saída, use algo como ls -q | command(s)
where -q
prints em ?
vez de caracteres descolados (como novas linhas) e command(s)
processe apenas N linhas de saída, imprima a enésima e depois pare.
Com cauda e cabeça (adicionado nl para maior clareza). Vamos buscar a quinta linha:
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 ~ $
Isso irá para a quinta linha:
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 ~ $
Então, obviamente, siga -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 "$@"
passarlsn
os argumentos dels
. Claro, depois de usarshift
para tirar$1
.