{"id":15622,"date":"2019-05-14T09:05:55","date_gmt":"2019-05-14T12:05:55","guid":{"rendered":"https:\/\/blog.hostinger.io\/br-tutoriais\/?p=15622"},"modified":"2023-04-12T08:42:53","modified_gmt":"2023-04-12T11:42:53","slug":"como-usar-comando-sed-linux","status":"publish","type":"post","link":"\/br\/tutoriais\/como-usar-comando-sed-linux","title":{"rendered":"Comando sed no Linux: uso e exemplos"},"content":{"rendered":"<p><span style=\"font-weight: 400\">O comando SED no Linux &eacute; uma ferramenta poderosa que ajuda a executar tarefas de uso geral. Entre elas: analisar e transformar textos. Hoje em dia, o SED &eacute; suportado nos principais sistemas operacionais.<\/span><\/p><p><span style=\"font-weight: 400\">A sintaxe e v&aacute;rios recursos &uacute;teis do SED s&atilde;o, em sua maioria, emprestados do editor<em> ed<\/em>. O mecanismo em si inclui fluxos de entrada padr&otilde;es, bem como de arquivos de texto. O script base da sintaxe do SED parece um pouco dif&iacute;cil no come&ccedil;o. No entanto, muitas tarefas complexas podem ser solucionadas com apenas algumas linhas de script SED.<\/span><\/p><figure class=\"wp-block-image size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheatsheet-BR.pdf\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/public\" alt=\"\" class=\"wp-image-30351\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2022\/12\/Linux-cheat-sheet-BR.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-instalacao-do-sed\"><b>Instala&ccedil;&atilde;o do SED<\/b><\/h2><p><span style=\"font-weight: 400\">Nos sistemas baseados em Linux, o SED &eacute; instalado por padr&atilde;o. Lembre-se, antes de come&ccedil;ar, voc&ecirc; deve acessar seu <\/span><a href=\"\/br\/servidor-vps\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"font-weight: 400\">VPS Linux<\/span><\/a><span style=\"font-weight: 400\"> com SSH. Confira nosso <\/span><a href=\"https:\/\/www.hostinger.com\/tutorials\/how-to-connect-to-your-account-using-putty\"><span style=\"font-weight: 400\">tutorial PuTTY<\/span><\/a><span style=\"font-weight: 400\"> se voc&ecirc; estiver com problemas. <\/span><\/p><p><span style=\"font-weight: 400\">O comando <\/span><b><i>which<\/i><\/b><span style=\"font-weight: 400\"> pode ser usado para verificar se o sistema possui SED ou n&atilde;o. No GNU\/Linux Debian, pode-se instalar o SED usando o apt package manager da seguinte forma:<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sudo apt-get install sed<\/pre><p>Para garantir que o SED esteja instalado corretamente, o seguinte comando &eacute; utilizado:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed --version<\/pre><p>O comando retorna o seguinte output:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sed (GNU sed) 4.2.2\nCopyright (C) 2012 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;.\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n \nWritten by Jay Fenlason, Tom Lord, Ken Pizzini,\nand Paolo Bonzini.\nGNU sed home page: &lt;http:\/\/www.gnu.org\/software\/sed\/&gt;.\nGeneral help using GNU software: &lt;http:\/\/www.gnu.org\/gethelp\/&gt;.\nE-mail bug reports to: &lt;bug-sed@gnu.org&gt;<\/pre><h2 class=\"wp-block-heading\" id=\"h-sed-workflow\"><b>SED <\/b><b><i>Workflow<\/i><\/b><\/h2><p><span style=\"font-weight: 400\">O <\/span><i><span style=\"font-weight: 400\">workflow<\/span><\/i><span style=\"font-weight: 400\"> b&aacute;sico do SED &eacute; leitura, execu&ccedil;&atilde;o e exibi&ccedil;&atilde;o, como mencionado na <\/span><b>Figura 1.<\/b><\/p><p><span style=\"font-weight: 400\">A leitura recebe os valores de entrada e armazena em um <\/span><b>buffer de padr&otilde;es<\/b><span style=\"font-weight: 400\">. A fase de execu&ccedil;&atilde;o realiza o comando no arquivo sequencialmente. E ap&oacute;s a execu&ccedil;&atilde;o, exibe o fluxo de sa&iacute;da. O buffer de padr&otilde;es &eacute; liberado conforme o conte&uacute;do &eacute; exibido na interface. O processo leitura, execu&ccedil;&atilde;o e exibi&ccedil;&atilde;o se repetem at&eacute; o final do arquivo.<\/span><\/p><h2 class=\"wp-block-heading\" id=\"h-exemplo-do-sed\"><b>Exemplo do SED <\/b><\/h2><p><span style=\"font-weight: 400\">Para explicar de forma simples, o SED l&ecirc; um arquivo e retorna seu texto. Para criar um arquivo de texto, use o editor <em>vi<\/em>.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ vi text.txtPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer Programs<\/pre><p><span style=\"font-weight: 400\">O comando para ler um arquivo de texto &eacute; o seguinte: <\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed &lsquo;&rsquo; text.txtPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer Programs<\/pre><p><span style=\"font-weight: 400\">O comando recebe entrada do arquivo &ldquo;<\/span><b>text.txt<\/b><span style=\"font-weight: 400\">&rdquo;. Antes de fazer o uploading de entrada do arquivo, escreva um argumento de linha de comando entre um par de aspas simples. Isso &ldquo;falar&aacute;&rdquo; para a linha do comando executar o SED.<\/span><\/p><p><span style=\"font-weight: 400\">O SED l&ecirc; o arquivo de entrada &ldquo;<\/span><b>text.txt<\/b><span style=\"font-weight: 400\">&rdquo; e armazena os dados em um buffer de padr&otilde;es. Ap&oacute;s isso, a opera&ccedil;&atilde;o pode ser realizada.<\/span><\/p><p><span style=\"font-weight: 400\">Neste caso, n&oacute;s passamos um argumento vazio para que nenhuma opera&ccedil;&atilde;o seja executada. O SED apresenta os dados armazenados como uma sa&iacute;da padr&atilde;o e esvazia o buffer de padr&otilde;es.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed &lsquo;&rsquo;Programs must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer ProgramsPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer Programs<\/pre><p><span style=\"font-weight: 400\">O comando SED recebe a entrada do teclado. Ele &eacute; exibido na primeira e segunda linha. Ele exibe os dados armazenados no buffer de padr&otilde;es. Para encerrar a sess&atilde;o do SED, use <\/span><b>CTRL+D <\/b><span style=\"font-weight: 400\">no teclado.<\/span><\/p><h2 class=\"wp-block-heading\" id=\"h-comandos-basicos\"><b>Comandos b&aacute;sicos<\/b><\/h2><p><span style=\"font-weight: 400\">Vamos aprender os comandos b&aacute;sicos do SED do Linux:<\/span><\/p><h3 class=\"wp-block-heading\" id=\"h-comando-de-deletar\"><span style=\"font-weight: 400\">Comando de deletar<\/span><\/h3><p><span style=\"font-weight: 400\">Para executar o comando de deletar, use <\/span><b><i>d<\/i><\/b><span style=\"font-weight: 400\"> junto com um arquivo entre aspas. O comando ir&aacute; deletar a primeira linha no arquivo <\/span><b><i>text.txt<\/i><\/b><span style=\"font-weight: 400\">.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed &lsquo;1d&rsquo; text.txt<\/pre><h3 class=\"wp-block-heading\" id=\"h-comando-de-escrita\"><strong>Comando de Escrita<\/strong><\/h3><p><span style=\"font-weight: 400\">Para executar o comando de escrita, escreva <\/span><b><i>w<\/i><\/b><span style=\"font-weight: 400\">, o n&uacute;mero da linha, e o arquivo, entre aspas. O seguinte comando l&ecirc; a segunda linha e a escreve no arquivo <\/span><b><i>text2.txt<\/i><\/b><span style=\"font-weight: 400\">.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed '2~2 w text2.txt' text.txt  \n[user]: ~$ cat text2.txt\n \nHarold Abelson, Structure and Interpretation of Computer Programs<\/pre><h3 class=\"wp-block-heading\" id=\"h-comando-de-acrescentar\"><strong>Comando de Acrescentar<\/strong><\/h3><p><span style=\"font-weight: 400\">Use a palavra-chave e o n&uacute;mero da linha entre aspas. Ap&oacute;s fechar as aspas, forne&ccedil;a a fonte a ser acrescentada. O seguinte comando acrescentar&aacute; depois da segunda linha no arquivo <\/span><b><i>text.txt<\/i><\/b><span style=\"font-weight: 400\"> arquivo.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed '2 a The Append example' text.txt[user]: ~$ cat text.txtPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer ProgramsThe Append example'<\/pre><h3 class=\"wp-block-heading\" id=\"h-comando-de-leitura\"><strong>Comando de Leitura<\/strong><\/h3><p><span style=\"font-weight: 400\">Use <\/span><b><i>r<\/i><\/b> <span style=\"font-weight: 400\">e digite a localiza&ccedil;&atilde;o do arquivo entre aspas. O seguinte comando ir&aacute; ler a entrada do arquivo de texto e acrescentar depois da terceira linha no arquivo <\/span><b><i>text2.txt<\/i><\/b><span style=\"font-weight: 400\">.<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[user]: ~$ sed '3 r text.txt' text2.txt\n[user]: ~$ cat text2.txt\n \nPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer Programs\nThe Append example'\nPrograms must be written for people to read, and only incidentally for machines to execute.&rdquo; &#8213; Harold Abelson, Structure and Interpretation of Computer Programs<\/pre><figure class=\"wp-block-image size-large\"><a href=\"\/br\/servidor-vps\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/public\" alt=\"\" class=\"wp-image-32923\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/53\/2023\/02\/BR-VPS-hosting_in-text-banner-1.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusao\"><b>Conclus&atilde;o<\/b><\/h2><p><span style=\"font-weight: 400\">Neste tutorial, voc&ecirc; aprendeu sobre a instala&ccedil;&atilde;o do SED e opera&ccedil;&otilde;es b&aacute;sicas. A ferramenta SED permite que o usu&aacute;rio utilize a linha de comando de diversas maneiras.<\/span><\/p><p><span style=\"font-weight: 400\">Voc&ecirc; tamb&eacute;m poder&aacute; resolver problemas complexos de maneira eficiente. Al&eacute;m do SED, o GNU-Linux oferece muitas ferramentas &uacute;teis para realizar tarefas cotidianas.<\/span><\/p><p>Para fechar, expanda seus conhecimentos sobre <a href=\"\/br\/tutoriais\/o-que-e-vps-como-escolher-um-servidor-vps\" target=\"_blank\" rel=\"noreferrer noopener\">o que &eacute; VPS<\/a> e como este tipo de hospedagem ajuda seu site a decolar e a se estabelecer na internet. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>O comando SED no Linux &eacute; uma ferramenta poderosa que ajuda a executar tarefas de uso geral. Entre elas: analisar e transformar textos. Hoje em dia, o SED &eacute; suportado nos principais sistemas operacionais. A sintaxe e v&aacute;rios recursos &uacute;teis do SED s&atilde;o, em sua maioria, emprestados do editor ed. O mecanismo em si inclui [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/br\/tutoriais\/como-usar-comando-sed-linux\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":78,"featured_media":15623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_title":"","rank_math_description":"Um guia detalhado sobre como usar um utilit\u00e1rio terminal essencial - o comando sed do Linux. Veja tudo o que voc\u00ea precisa saber.","rank_math_focus_keyword":"comando sed","footnotes":""},"categories":[4915],"tags":[6532,6534,6533],"class_list":["post-15622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps","tag-comando-sed","tag-comando-sed-no-linux","tag-sed-no-linux"],"hreflangs":[{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-usar-comando-sed-linux","default":1},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-usar-comando-sed-linux","default":0}],"acf":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/posts\/15622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/comments?post=15622"}],"version-history":[{"count":7,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/posts\/15622\/revisions"}],"predecessor-version":[{"id":34093,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/posts\/15622\/revisions\/34093"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/media\/15623"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/media?parent=15622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/categories?post=15622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/br\/tutoriais\/wp-json\/wp\/v2\/tags?post=15622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}