Discussion:
[shell-script] SED - Como parar a consulta na N ocorrência
andrelucio.fc@yahoo.com [shell-script]
2017-08-22 13:04:47 UTC
Permalink
Opa, pessoal.


Seguinte, existe alguma forma de parar a consulta de uma determinada string na ocorrência N dessa consulta.
Por exemplo:


O arquivo ips.txt tem esse conteúdo:

192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)


O comando sed -n '/\<192.168.1.4\>/p' ips.txt retorna essa saída:

192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)


Mas preciso que ele exiba somente a segunda ocorrência ou seja, essa saída:


192.168.1.4 (PC)
192.168.1.4 (Smartphone)

Alguém poderia saberia como resolver isso com o SED?
itamarnet@yahoo.com.br [shell-script]
2017-08-22 13:29:21 UTC
Permalink
Caro André


Nesse caso acho melhor usar o grep:

grep -m 2 '\b192.168.1.4\b' ips.txt





[]'s
Itamar
Tiago Peczenyj tiago.peczenyj@gmail.com [shell-script]
2017-08-22 13:34:30 UTC
Permalink
wow

em tantos anos eu nunca usei esta opção

-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input
is standard input from a regular file, and NUM matching lines are output,
grep ensures that the standard input is positioned
to just after the last matching line before exiting,
regardless of the presence of trailing context lines. This enables a
calling process to resume a search. When grep stops after
NUM matching lines, it outputs any trailing context
lines. When the -c or --count option is also used, grep does not output a
count greater than NUM. When the -v or --invert-match
option is also used, grep stops after outputting NUM
non-matching lines.
Post by ***@yahoo.com.br [shell-script]
Caro André
grep -m 2 '\b192.168.1.4\b' ips.txt
[]'s
Itamar
--
Tiago B. Peczenyj

http://about.me/peczenyj
'Julio C. Neves' julio.neves@gmail.com [shell-script]
2017-08-22 20:53:22 UTC
Permalink
Itamar é leitor voraz do man e do que aparecer pela frente ;)

Olha o que escrevi sobre ele no meu livro no capítulo que fala de sed e
grep:
==========================

O Itamar Santos de Souza, PhD em assuntos “sedianos e awkdianos” e que pode
ser achado dando pitacos na lista s*hell*-*script* do *Yahoo* muito me
ajudou neste capítulo, revisando-o e sugerindo dicas. Valeu, Itamar!
==========================
em outra parte escrevi:
=========================

E o Itamar Santos de Souza, um colega da lista que sabe tudo e mais um
pouco sobre sed e awk, respondeu:

Pode usar os navegadores modo texto e depois filtrar com sed, awk, cut, etc.
Por exemplo:

*echo "<tr><td><strong>Tensão de entrada: \*

*</strong></td><td>218.9 V</td></tr>" \*

*| links -dump*

ou:

*echo "<tr><td><strong>Tensão de entrada: \*

*</strong></td><td>218.9 V</td></tr>" \*

*| lynx -dump -stdin*

ou ainda:

*echo "<tr><td><strong>Tensão de entrada: \*

*</strong></td><td>218.9 V</td></tr>" \*

*| w3m -T text/html*

E para tirar os números desejados bastaria concatenar qualquer um desses
casos em um *pipe* com um simples sed.

*"Um dos 3 comandos anteriores" | sed 's/[^0-9.]//g'*
=========================

Valeu Itamar!!!!!

Em Agosto darei um treinamento de 40 horas no RJ e em BSB sobre
Programação em Shell, com imersão em Expressões Regulares e dicas
de YAD. Para mais detalhes acesse goo.gl/JkWmBx
Em Dezembro um curso com este mesmo conteúdo/carga horária será
lecionado em SP. Maiores detalhes neste site.
<https://www.4linux.com.br/curso/programacao-em-shell-script>



Abcs,
Julio

*Damos treinamento em sua empresa por **um preço, *
*no mínimo, 50% mais barato que qualquer curso,*
*com certificado e nota fiscal.*

​​
Nosso time de instrutores *in company* é formado somente por
​
​
autores
​ ​
​
de
*Best Sellers​ ​*​laureados ​sobre os temas. P. exemplo:

Shell básico e Programação em Shell Julio Neves
Bacula Heitor Medrado
Zabbix Adail Host
Produção Gráfica e Videografismo Cadunico
Post by Tiago Peczenyj ***@gmail.com [shell-script]
wow
em tantos anos eu nunca usei esta opção
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input
is standard input from a regular file, and NUM matching lines are output,
grep ensures that the standard input is positioned
to just after the last matching line before exiting,
regardless of the presence of trailing context lines. This enables a
calling process to resume a search. When grep stops after
NUM matching lines, it outputs any trailing context
lines. When the -c or --count option is also used, grep does not output a
count greater than NUM. When the -v or --invert-match
option is also used, grep stops after outputting NUM
non-matching lines.
Post by ***@yahoo.com.br [shell-script]
Caro André
grep -m 2 '\b192.168.1.4\b' ips.txt
[]'s
Itamar
--
Tiago B. Peczenyj
http://about.me/peczenyj
phfbettega@yahoo.com.br [shell-script]
2017-08-22 14:05:24 UTC
Permalink
Olá andrelucio, esse sed adiciona todas as linhas com o endereço no espaço reserva
e depois imprime até a ocorrência N que é o número do \n que fica fora do grupo.

sed -nr '/\<192.168.1.4\>/ H; $ {g;s/\n//;s/([^\n]+\n[^\n]+)\n.*/\1/p}' ips.txt

Abraços Paulo
Post by ***@yahoo.com [shell-script]
Opa, pessoal.
Seguinte, existe alguma forma de parar a consulta de uma determinada string na ocorrência N dessa consulta.
192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
Alguém poderia saberia como resolver isso com o SED?
--------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
João Paulo Ramos Nogueira jprnogueira@yahoo.com.br [shell-script]
2017-08-23 11:07:50 UTC
Permalink
Pode usar assim tambem~:  sed -n '1,2p' '/\<192.168.1.4\>/p' ips.txt
Sent from Yahoo Mail on Android


On Tue, Aug 22, 2017 at 11:05, ***@yahoo.com.br [shell-script]<shell-***@yahoogrupos.com.br> wrote:  
Olá andrelucio, esse sed adiciona todas as linhas com o endereço no espaço reserva
e depois imprime até a ocorrência N que é o número do \n que fica fora do grupo.

sed -nr '/\<192.168.1.4\>/ H; $ {g;s/\n//;s/([^\n]+\n[^\n]+)\n.*/\1/p}' ips.txt

Abraços Paulo
Post by ***@yahoo.com [shell-script]
Opa, pessoal.
Seguinte, existe alguma forma de parar a consulta de uma determinada string na ocorrência N dessa consulta.
192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
Alguém poderia saberia como resolver isso com o SED?
----------------------------------------------------------
----------------------------------------------------------
#yiv8194577218 #yiv8194577218 -- #yiv8194577218ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv8194577218 #yiv8194577218ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv8194577218 #yiv8194577218ygrp-mkp #yiv8194577218hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv8194577218 #yiv8194577218ygrp-mkp #yiv8194577218ads {margin-bottom:10px;}#yiv8194577218 #yiv8194577218ygrp-mkp .yiv8194577218ad {padding:0 0;}#yiv8194577218 #yiv8194577218ygrp-mkp .yiv8194577218ad p {margin:0;}#yiv8194577218 #yiv8194577218ygrp-mkp .yiv8194577218ad a {color:#0000ff;text-decoration:none;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ygrp-lc {font-family:Arial;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ygrp-lc #yiv8194577218hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ygrp-lc .yiv8194577218ad {margin-bottom:10px;padding:0 0;}#yiv8194577218 #yiv8194577218actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv8194577218 #yiv8194577218activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv8194577218 #yiv8194577218activity span {font-weight:700;}#yiv8194577218 #yiv8194577218activity span:first-child {text-transform:uppercase;}#yiv8194577218 #yiv8194577218activity span a {color:#5085b6;text-decoration:none;}#yiv8194577218 #yiv8194577218activity span span {color:#ff7900;}#yiv8194577218 #yiv8194577218activity span .yiv8194577218underline {text-decoration:underline;}#yiv8194577218 .yiv8194577218attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv8194577218 .yiv8194577218attach div a {text-decoration:none;}#yiv8194577218 .yiv8194577218attach img {border:none;padding-right:5px;}#yiv8194577218 .yiv8194577218attach label {display:block;margin-bottom:5px;}#yiv8194577218 .yiv8194577218attach label a {text-decoration:none;}#yiv8194577218 blockquote {margin:0 0 0 4px;}#yiv8194577218 .yiv8194577218bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv8194577218 .yiv8194577218bold a {text-decoration:none;}#yiv8194577218 dd.yiv8194577218last p a {font-family:Verdana;font-weight:700;}#yiv8194577218 dd.yiv8194577218last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv8194577218 dd.yiv8194577218last p span.yiv8194577218yshortcuts {margin-right:0;}#yiv8194577218 div.yiv8194577218attach-table div div a {text-decoration:none;}#yiv8194577218 div.yiv8194577218attach-table {width:400px;}#yiv8194577218 div.yiv8194577218file-title a, #yiv8194577218 div.yiv8194577218file-title a:active, #yiv8194577218 div.yiv8194577218file-title a:hover, #yiv8194577218 div.yiv8194577218file-title a:visited {text-decoration:none;}#yiv8194577218 div.yiv8194577218photo-title a, #yiv8194577218 div.yiv8194577218photo-title a:active, #yiv8194577218 div.yiv8194577218photo-title a:hover, #yiv8194577218 div.yiv8194577218photo-title a:visited {text-decoration:none;}#yiv8194577218 div#yiv8194577218ygrp-mlmsg #yiv8194577218ygrp-msg p a span.yiv8194577218yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv8194577218 .yiv8194577218green {color:#628c2a;}#yiv8194577218 .yiv8194577218MsoNormal {margin:0 0 0 0;}#yiv8194577218 o {font-size:0;}#yiv8194577218 #yiv8194577218photos div {float:left;width:72px;}#yiv8194577218 #yiv8194577218photos div div {border:1px solid #666666;min-height:62px;overflow:hidden;width:62px;}#yiv8194577218 #yiv8194577218photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv8194577218 #yiv8194577218reco-category {font-size:77%;}#yiv8194577218 #yiv8194577218reco-desc {font-size:77%;}#yiv8194577218 .yiv8194577218replbq {margin:4px;}#yiv8194577218 #yiv8194577218ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv8194577218 #yiv8194577218ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv8194577218 #yiv8194577218ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv8194577218 #yiv8194577218ygrp-mlmsg select, #yiv8194577218 input, #yiv8194577218 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv8194577218 #yiv8194577218ygrp-mlmsg pre, #yiv8194577218 code {font:115% monospace;}#yiv8194577218 #yiv8194577218ygrp-mlmsg * {line-height:1.22em;}#yiv8194577218 #yiv8194577218ygrp-mlmsg #yiv8194577218logo {padding-bottom:10px;}#yiv8194577218 #yiv8194577218ygrp-msg p a {font-family:Verdana;}#yiv8194577218 #yiv8194577218ygrp-msg p#yiv8194577218attach-count span {color:#1E66AE;font-weight:700;}#yiv8194577218 #yiv8194577218ygrp-reco #yiv8194577218reco-head {color:#ff7900;font-weight:700;}#yiv8194577218 #yiv8194577218ygrp-reco {margin-bottom:20px;padding:0px;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ov li a {font-size:130%;text-decoration:none;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv8194577218 #yiv8194577218ygrp-sponsor #yiv8194577218ov ul {margin:0;padding:0 0 0 8px;}#yiv8194577218 #yiv8194577218ygrp-text {font-family:Georgia;}#yiv8194577218 #yiv8194577218ygrp-text p {margin:0 0 1em 0;}#yiv8194577218 #yiv8194577218ygrp-text tt {font-size:120%;}#yiv8194577218 #yiv8194577218ygrp-vital ul li:last-child {border-right:none !important;}#yiv8194577218
João Paulo Ramos Nogueira jprnogueira@yahoo.com.br [shell-script]
2017-08-23 12:06:00 UTC
Permalink
desculpa, pode usar assim:sed -n '/\<192.168.1.4\>/p' ips.txt | sed -n '1,2p'Sent from Yahoo Mail on Android

On Wed, Aug 23, 2017 at 8:22, João Paulo Ramos Nogueira ***@yahoo.com.br [shell-script]<shell-***@yahoogrupos.com.br> wrote:  
Pode usar assim tambem~:  sed -n '1,2p' '/\<192.168.1.4\>/p' ips.txt

Sent from Yahoo Mail on Android


On Tue, Aug 22, 2017 at 11:05, ***@yahoo.com.br [shell-script]<shell-***@yahoogrupos.com.br> wrote:  
Olá andrelucio, esse sed adiciona todas as linhas com o endereço no espaço reserva
e depois imprime até a ocorrência N que é o número do \n que fica fora do grupo.

sed -nr '/\<192.168.1.4\>/ H; $ {g;s/\n//;s/([^\n]+\n[^\n]+)\n.*/\1/p}' ips.txt

Abraços Paulo
Post by ***@yahoo.com [shell-script]
Opa, pessoal.
Seguinte, existe alguma forma de parar a consulta de uma determinada string na ocorrência N dessa consulta.
192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
Alguém poderia saberia como resolver isso com o SED?
----------------------------------------------------------
----------------------------------------------------------
#yiv5597250680 #yiv5597250680 -- #yiv5597250680ygrp-mkp {border:1px solid #d8d8d8;font-family:Arial;margin:10px 0;padding:0 10px;}#yiv5597250680 #yiv5597250680ygrp-mkp hr {border:1px solid #d8d8d8;}#yiv5597250680 #yiv5597250680ygrp-mkp #yiv5597250680hd {color:#628c2a;font-size:85%;font-weight:700;line-height:122%;margin:10px 0;}#yiv5597250680 #yiv5597250680ygrp-mkp #yiv5597250680ads {margin-bottom:10px;}#yiv5597250680 #yiv5597250680ygrp-mkp .yiv5597250680ad {padding:0 0;}#yiv5597250680 #yiv5597250680ygrp-mkp .yiv5597250680ad p {margin:0;}#yiv5597250680 #yiv5597250680ygrp-mkp .yiv5597250680ad a {color:#0000ff;text-decoration:none;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ygrp-lc {font-family:Arial;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ygrp-lc #yiv5597250680hd {margin:10px 0px;font-weight:700;font-size:78%;line-height:122%;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ygrp-lc .yiv5597250680ad {margin-bottom:10px;padding:0 0;}#yiv5597250680 #yiv5597250680actions {font-family:Verdana;font-size:11px;padding:10px 0;}#yiv5597250680 #yiv5597250680activity {background-color:#e0ecee;float:left;font-family:Verdana;font-size:10px;padding:10px;}#yiv5597250680 #yiv5597250680activity span {font-weight:700;}#yiv5597250680 #yiv5597250680activity span:first-child {text-transform:uppercase;}#yiv5597250680 #yiv5597250680activity span a {color:#5085b6;text-decoration:none;}#yiv5597250680 #yiv5597250680activity span span {color:#ff7900;}#yiv5597250680 #yiv5597250680activity span .yiv5597250680underline {text-decoration:underline;}#yiv5597250680 .yiv5597250680attach {clear:both;display:table;font-family:Arial;font-size:12px;padding:10px 0;width:400px;}#yiv5597250680 .yiv5597250680attach div a {text-decoration:none;}#yiv5597250680 .yiv5597250680attach img {border:none;padding-right:5px;}#yiv5597250680 .yiv5597250680attach label {display:block;margin-bottom:5px;}#yiv5597250680 .yiv5597250680attach label a {text-decoration:none;}#yiv5597250680 blockquote {margin:0 0 0 4px;}#yiv5597250680 .yiv5597250680bold {font-family:Arial;font-size:13px;font-weight:700;}#yiv5597250680 .yiv5597250680bold a {text-decoration:none;}#yiv5597250680 dd.yiv5597250680last p a {font-family:Verdana;font-weight:700;}#yiv5597250680 dd.yiv5597250680last p span {margin-right:10px;font-family:Verdana;font-weight:700;}#yiv5597250680 dd.yiv5597250680last p span.yiv5597250680yshortcuts {margin-right:0;}#yiv5597250680 div.yiv5597250680attach-table div div a {text-decoration:none;}#yiv5597250680 div.yiv5597250680attach-table {width:400px;}#yiv5597250680 div.yiv5597250680file-title a, #yiv5597250680 div.yiv5597250680file-title a:active, #yiv5597250680 div.yiv5597250680file-title a:hover, #yiv5597250680 div.yiv5597250680file-title a:visited {text-decoration:none;}#yiv5597250680 div.yiv5597250680photo-title a, #yiv5597250680 div.yiv5597250680photo-title a:active, #yiv5597250680 div.yiv5597250680photo-title a:hover, #yiv5597250680 div.yiv5597250680photo-title a:visited {text-decoration:none;}#yiv5597250680 div#yiv5597250680ygrp-mlmsg #yiv5597250680ygrp-msg p a span.yiv5597250680yshortcuts {font-family:Verdana;font-size:10px;font-weight:normal;}#yiv5597250680 .yiv5597250680green {color:#628c2a;}#yiv5597250680 .yiv5597250680MsoNormal {margin:0 0 0 0;}#yiv5597250680 o {font-size:0;}#yiv5597250680 #yiv5597250680photos div {float:left;width:72px;}#yiv5597250680 #yiv5597250680photos div div {border:1px solid #666666;min-height:62px;overflow:hidden;width:62px;}#yiv5597250680 #yiv5597250680photos div label {color:#666666;font-size:10px;overflow:hidden;text-align:center;white-space:nowrap;width:64px;}#yiv5597250680 #yiv5597250680reco-category {font-size:77%;}#yiv5597250680 #yiv5597250680reco-desc {font-size:77%;}#yiv5597250680 .yiv5597250680replbq {margin:4px;}#yiv5597250680 #yiv5597250680ygrp-actbar div a:first-child {margin-right:2px;padding-right:5px;}#yiv5597250680 #yiv5597250680ygrp-mlmsg {font-size:13px;font-family:Arial, helvetica, clean, sans-serif;}#yiv5597250680 #yiv5597250680ygrp-mlmsg table {font-size:inherit;font:100%;}#yiv5597250680 #yiv5597250680ygrp-mlmsg select, #yiv5597250680 input, #yiv5597250680 textarea {font:99% Arial, Helvetica, clean, sans-serif;}#yiv5597250680 #yiv5597250680ygrp-mlmsg pre, #yiv5597250680 code {font:115% monospace;}#yiv5597250680 #yiv5597250680ygrp-mlmsg * {line-height:1.22em;}#yiv5597250680 #yiv5597250680ygrp-mlmsg #yiv5597250680logo {padding-bottom:10px;}#yiv5597250680 #yiv5597250680ygrp-msg p a {font-family:Verdana;}#yiv5597250680 #yiv5597250680ygrp-msg p#yiv5597250680attach-count span {color:#1E66AE;font-weight:700;}#yiv5597250680 #yiv5597250680ygrp-reco #yiv5597250680reco-head {color:#ff7900;font-weight:700;}#yiv5597250680 #yiv5597250680ygrp-reco {margin-bottom:20px;padding:0px;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ov li a {font-size:130%;text-decoration:none;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ov li {font-size:77%;list-style-type:square;padding:6px 0;}#yiv5597250680 #yiv5597250680ygrp-sponsor #yiv5597250680ov ul {margin:0;padding:0 0 0 8px;}#yiv5597250680 #yiv5597250680ygrp-text {font-family:Georgia;}#yiv5597250680 #yiv5597250680ygrp-text p {margin:0 0 1em 0;}#yiv5597250680 #yiv5597250680ygrp-text tt {font-size:120%;}#yiv5597250680 #yiv5597250680ygrp-vital ul li:last-child {border-right:none !important;}#yiv5597250680
phfbettega@yahoo.com.br [shell-script]
2017-08-23 13:40:23 UTC
Permalink
Pessoal, me desculpem pelo sed que eu postei, é terrivelmente ineficiente.
Além de acumular todas as linhas com o ip só pra pegar as duas primeiras,
ainda por cima espera até o final do arquivo pra apresentar o resultado.
Pensei num sed que funcione como a opção -m do grep

sed -nr '/\<192.168.1.4\>/ {H;g;s/^((\n[^\n]+){2})/\1/;ta;d;:a;s/^\n//;p;q}' ips.txt

vai testando a cada acumulação no espaço reserva, se houver tantas repetições
indicadas nas chaves, imprime e sai, senão deleta o espaço padrão e inicia um novo ciclo.

Abraços Paulo
Post by ***@yahoo.com.br [shell-script]
Olá andrelucio, esse sed adiciona todas as linhas com o endereço no espaço reserva
e depois imprime até a ocorrência N que é o número do \n que fica fora do grupo.
sed -nr '/\<192.168.1.4\>/ H; $ {g;s/\n//;s/([^\n]+\n[^\n]+)\n.*/\1/p}' ips.txt
Abraços Paulo
Post by ***@yahoo.com [shell-script]
Opa, pessoal.
Seguinte, existe alguma forma de parar a consulta de uma determinada string na ocorrência N dessa consulta.
192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
Alguém poderia saberia como resolver isso com o SED?
----------------------------------------------------------
----------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
Tiago Peczenyj tiago.peczenyj@gmail.com [shell-script]
2017-08-22 13:12:02 UTC
Permalink
Ola

vc quer uma maquina de estados, e fazer isso com SED pode até ser possivel,
mas eu não sei se seria pratico

vc pode usar alguma linguagem de proposito geral e que seja legivel como
AWK e criar uma variavel que incrementa ate o numero que vc quer e ai vc
simplesmente "termina".

outra forma é criar um pipe do sed original para um head que vai parar em
uma determinada linha, exemplo:

sed 'busca' | head -n 5

dessa forma apos a quinta linha o processo head vai terminar e vai mandar
um SIGPIPE para o sed, que vai terminar também.

para ser mais eficiente talvez vc queira substituir o sed por um grep.

um awk pode fazer em uma tacada só - em SED eu vou ficar devendo, mas
certamente tem jeito
Post by ***@yahoo.com [shell-script]
Opa, pessoal.
Seguinte, existe alguma forma de parar a consulta de uma determinada
string na ocorrência N dessa consulta.
192.168.1.4 (PC)
192.168.0.25 (PC)
192.168.1.4 (Smartphone)
192.168.0.30 (Smartphone)
192.168.1.40 (PC)
192.168.1.4 (Server)
192.168.1.10 (PC)
192.168.1.50 (Smartphone)
192.168.1.20 (PC)
192.168.0.222 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
192.168.1.4 (Server)
192.168.1.4 (PC)
192.168.1.4 (Smartphone)
Alguém poderia saberia como resolver isso com o SED?
--
Tiago B. Peczenyj

http://about.me/peczenyj
itamarnet@yahoo.com.br [shell-script]
2017-08-22 18:01:32 UTC
Permalink
Tiago

Uma opção em awk seria essa:


awk '/\<192.168.1.4\>/{print; i++; if (i>=2) exit}' ips.txt





[]'s
Itamar
andrelucio.fc@yahoo.com [shell-script]
2017-08-22 18:48:13 UTC
Permalink
Opa,

Obrigado a todos pela ajuda.


Tiago, eu trabalho com linux embarcado, e ele não possui linguagens de programação, só consigo utilizar


Eu só consigo utilizar ou sed ou awk.


Itamar, as suas soluções vieram bem me ajudaram para essa solução. Vlw.
andrelucio.fc@yahoo.com [shell-script]
2017-08-22 18:48:51 UTC
Permalink
Obrigado pela ajuda, Paulo. Vlw.
Continue reading on narkive:
Loading...