Difference between revisions of "Sed (editor)"

From wikieduonline
Jump to navigation Jump to search
 
(30 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{lowercase}}
 
<code>[[wikipedia:sed]]</code> ([[1974]]) utility is using for text processing, such as strings replacements.
 
<code>[[wikipedia:sed]]</code> ([[1974]]) utility is using for text processing, such as strings replacements.
 +
* Man: https://manpages.ubuntu.com/manpages/noble/en/man1/sed.1.html
 
* Man: https://man7.org/linux/man-pages/man1/sed.1p.html
 
* Man: https://man7.org/linux/man-pages/man1/sed.1p.html
  
 +
sed /xxx/yyy/g
 +
 +
  g - global
 +
[[sed -i|-i]]  -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied)
 +
[[sed -e|-e]]
 +
 +
  -E, -r, --regexp-extended use extended regular expressions in the script (for portability use [[POSIX -E]]).
 +
 +
[[brew install gnu-sed]]
  
  
 
== Sed Basic examples ==
 
== Sed Basic examples ==
  
* [[Replace]] all occurrences of TEXT_STRING
+
* [[Replace]] all occurrences of TEXT_STRING (<code>/g</code>)
:: <code>sed -i s/OLD_TEXT_STRING/NEW_TEXT_STRING/g file.txt</code>
+
:: <code>[[sed -i]] s/OLD_TEXT_STRING/NEW_TEXT_STRING/g file.txt</code>
  
 
* [[Append]] a text (TEST_TO_APPEND_AT_BEGINNIG) at the [[beginning]] of a line:
 
* [[Append]] a text (TEST_TO_APPEND_AT_BEGINNIG) at the [[beginning]] of a line:
 
:: <code>sed -i s/^/TEST_TO_APPEND_AT_BEGINNIG/ file.txt</code>
 
:: <code>sed -i s/^/TEST_TO_APPEND_AT_BEGINNIG/ file.txt</code>
  
:: Append a space at the beggining of lines:
+
:: [[Append a space]] at the beginning of lines:
 
::: <code>[[sed s/^./" "/]]</code>
 
::: <code>[[sed s/^./" "/]]</code>
  
* Append a text (TEST_TO_APPEND_AT_END) at the [[end of a line]] (<code>$</code>):
+
* Append a text (TEST_TO_APPEND_AT_END) at the [[end of a line]] (<[[code]]>$</code>):
 
:: <code>sed -i s/$/TEST_TO_APPEND_AT_END/ file.txt</code>
 
:: <code>sed -i s/$/TEST_TO_APPEND_AT_END/ file.txt</code>
  
Line 23: Line 34:
 
* Add  at the beginning and end of a sentence: <pre><code> and </code></pre>
 
* Add  at the beginning and end of a sentence: <pre><code> and </code></pre>
 
<pre>sed s/'^'/'\<code\>'/ | sed s/'$'/'\<\/code\>'/</pre>
 
<pre>sed s/'^'/'\<code\>'/ | sed s/'$'/'\<\/code\>'/</pre>
 +
 +
sed s/^/"[["/ | sed s/$/"]]"/
 +
 +
* Add * at the beginning of the line:
 +
::<code>[[sed]] s/^/"*"/ </code>
  
 
* Replace [[spaces]]:  
 
* Replace [[spaces]]:  
 
:<code>[[sed]] s/' '//g</code>
 
:<code>[[sed]] s/' '//g</code>
 +
 +
* Delete characters before a pattern
 +
[[sed -e]] 's/....yourtext/ yourtext/g' file.txt
 +
 +
* sed -Ei 's/^# deb-src /deb-src /' [[/etc/apt/sources.list]]
  
 
== Help ==
 
== Help ==
Line 31: Line 52:
 
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
 
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
  
== Related commands ==
+
== Related ==
 
* <code>[[awk]]</code>
 
* <code>[[awk]]</code>
 
* <code>[[lineinfile (Ansible)]]</code>
 
* <code>[[lineinfile (Ansible)]]</code>
Line 38: Line 59:
 
* <code>cat your_file.txt | [[while read]] i; do echo "$(date) $i"; done</code>
 
* <code>cat your_file.txt | [[while read]] i; do echo "$(date) $i"; done</code>
 
* <code>[[sed -n]]</code>
 
* <code>[[sed -n]]</code>
* [[Chef: Editing a file]]
+
* [[Chef: Editing a file]]: <code>[[replace_or_add]]</code>
 +
* [[Redirection]]: [[double redirection]]
 +
* [[file (Puppet)]]
 +
* [[prepend]]
 +
* [[kubectl patch]]
 +
* [[gsub]] [[gwak]]
 +
* <code>[[envsubst]]</code>
  
 
== See also ==
 
== See also ==
Line 44: Line 71:
 
* {{vi}}
 
* {{vi}}
 
* {{grep}}
 
* {{grep}}
* [[Ansible]] <code>[[lineinfile]]</code> [[DevOps/Ansible/Modules|module]]
 
 
  
 
[[Category:Linux commands]]
 
[[Category:Linux commands]]

Latest revision as of 14:42, 4 April 2024

wikipedia:sed (1974) utility is using for text processing, such as strings replacements.

sed /xxx/yyy/g
  g - global 
-i  -i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if SUFFIX supplied)
-e
  -E, -r, --regexp-extended use extended regular expressions in the script (for portability use POSIX -E).
brew install gnu-sed


Sed Basic examples[edit]

  • Replace all occurrences of TEXT_STRING (/g)
sed -i s/OLD_TEXT_STRING/NEW_TEXT_STRING/g file.txt
sed -i s/^/TEST_TO_APPEND_AT_BEGINNIG/ file.txt
Append a space at the beginning of lines:
sed s/^./" "/
sed -i s/$/TEST_TO_APPEND_AT_END/ file.txt
  • Move N lines from text file to new file:
head -1000 input > output && sed -i '1,+999d' input [1]
  • Add at the beginning and end of a sentence:
    <code> and </code>
sed s/'^'/'\<code\>'/ | sed s/'$'/'\<\/code\>'/
sed s/^/" sed s/$/""/
  • Add * at the beginning of the line:
sed s/^/"*"/
sed s/' '//g
  • Delete characters before a pattern
sed -e 's/....yourtext/ yourtext/g' file.txt

Help[edit]

usage: sed script [-Ealn] [-i extension] [file ...]
      sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

Related[edit]

See also[edit]

  • https://stackoverflow.com/a/801036
  • Advertising: