snipt

Ctrl+h for KB shortcuts

Bash

sed append to match

1
2
3
4
file="/path/to/file"
regex="find this .*"
suffix="-suffix"
sed -i "s/$regex/&$suffix/" $file
https://snipt.net/embed/940f084dd40897180b5a15ed7a27be6c/
https://snipt.net/raw/940f084dd40897180b5a15ed7a27be6c/
940f084dd40897180b5a15ed7a27be6c
bash
Bash
4
2016-09-28T22:29:46
True
False
False
Oct 13, 2015 at 06:33 AM
/api/public/snipt/141656/
sed-append-to-match
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nv">file</span><span class="o">=</span><span class="s2">&quot;/path/to/file&quot;</span> </span><span id="L-2"><a name="L-2"></a><span class="nv">regex</span><span class="o">=</span><span class="s2">&quot;find this .*&quot;</span> </span><span id="L-3"><a name="L-3"></a><span class="nv">suffix</span><span class="o">=</span><span class="s2">&quot;-suffix&quot;</span> </span><span id="L-4"><a name="L-4"></a>sed -i <span class="s2">&quot;s/</span><span class="nv">$regex</span><span class="s2">/&amp;</span><span class="nv">$suffix</span><span class="s2">/&quot;</span> <span class="nv">$file</span> </span></pre></div> </td></tr></table>
append, bash, linux, regex, sed

Markdown

Transform file list and more sed tricks

This command will print the filename and save them filtering only the part before the underscore.

ls| sed 's/_/ /g' | awk 'BEGIN {} $1 ~ /[0-9]/ {print $1}' > ../subcodes.txt
https://snipt.net/embed/d1cc07b2865af9f79b029efd2bb24313/
https://snipt.net/raw/d1cc07b2865af9f79b029efd2bb24313/
d1cc07b2865af9f79b029efd2bb24313
markdown
Markdown
3
2016-09-28T15:35:56
True
False
False
Mar 25, 2014 at 01:28 PM
/api/public/snipt/124715/
transform-file-list-and-more-sed-tricks
<p>This command will print the filename and save them filtering only the part before the underscore.</p> <pre><code>ls| sed 's/_/ /g' | awk 'BEGIN {} $1 ~ /[0-9]/ {print $1}' &gt; ../subcodes.txt </code></pre>
awk, sed

Bash

replace X with Y in all files in a project using sed

sed -i '' -e 's/X/Y/g' $(find . -type f)
https://snipt.net/embed/c195316a2cdd5d8900d146a160c0aba7/
https://snipt.net/raw/c195316a2cdd5d8900d146a160c0aba7/
c195316a2cdd5d8900d146a160c0aba7
bash
Bash
1
2016-09-23T03:51:31
True
False
False
Feb 18, 2014 at 04:52 PM
/api/public/snipt/114908/
replace-x-with-y-in-all-files-in-a-project-using-sed
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>sed -i <span class="s1">&#39;&#39;</span> -e <span class="s1">&#39;s/X/Y/g&#39;</span> <span class="k">$(</span>find . -type f<span class="k">)</span> </span></pre></div> </td></tr></table>
replace, sed

Text only

Change filenames using sed

for f in *.jpg; do mv $f `echo $f | sed s/IMG/VACATION/`; done
https://snipt.net/embed/b63e55d147a67189bd8ce929d0a80521/
https://snipt.net/raw/b63e55d147a67189bd8ce929d0a80521/
b63e55d147a67189bd8ce929d0a80521
text
Text only
1
2016-09-21T12:02:36
True
False
False
Aug 15, 2013 at 05:01 PM
/api/public/snipt/74511/
change-filenames-using-sed
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>for f in *.jpg; do mv $f `echo $f | sed s/IMG/VACATION/`; done </span></pre></div> </td></tr></table>
sed

Bash

hilight lines in blue

<$FILE sed "s/\(.*$PATTERN.*\)/$(tput setaf 4)\1$(tput sgr0)/g"
https://snipt.net/embed/4c01c17ca8e4e1cdaecfb7def16dd043/
https://snipt.net/raw/4c01c17ca8e4e1cdaecfb7def16dd043/
4c01c17ca8e4e1cdaecfb7def16dd043
bash
Bash
1
2016-09-28T19:57:30
True
False
False
Jul 19, 2012 at 05:39 PM
/api/public/snipt/30532/
hilight-lines-in-blue
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>&lt;<span class="nv">$FILE</span> sed <span class="s2">&quot;s/\(.*</span><span class="nv">$PATTERN</span><span class="s2">.*\)/</span><span class="k">$(</span>tput setaf 4<span class="k">)</span><span class="s2">\1</span><span class="k">$(</span>tput sgr0<span class="k">)</span><span class="s2">/g&quot;</span> </span></pre></div> </td></tr></table>
bash, hilight, sed

Bash

Recursively replace BOM-Characters in files

find . -type f -exec sed -i '1 s/^\xef\xbb\xbf//' {} +
https://snipt.net/embed/7b23948a6d67efbc9c7475550756078d/
https://snipt.net/raw/7b23948a6d67efbc9c7475550756078d/
7b23948a6d67efbc9c7475550756078d
bash
Bash
1
2016-09-27T20:36:16
True
False
False
/api/public/snipt/30472/
recursively-replace-bom-characters-in-files
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>find . -type f -exec sed -i <span class="s1">&#39;1 s/^\xef\xbb\xbf//&#39;</span> <span class="o">{}</span> + </span></pre></div> </td></tr></table>
bom, recursively, replace, sed, shell, utf8

Text only

Strip top line from text file

sed 1d $1 > $2
https://snipt.net/embed/2caa4d18b4791d5078f9806912ab1deb/
https://snipt.net/raw/2caa4d18b4791d5078f9806912ab1deb/
2caa4d18b4791d5078f9806912ab1deb
text
Text only
1
2016-09-28T06:35:14
True
False
False
/api/public/snipt/27046/
strip-top-line-from-text-file
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>sed 1d $1 &gt; $2 </span></pre></div> </td></tr></table>
sed, text
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.