Search ΒΆ
-
Count the number of lines with at least one occurrence of the
y
character:$ cat test.txt asd dd :; > y YYYyy yyy . asdkjlyes kjkjhkjhy $ grep -o '.*y.*' ./test.txt | wc -l 3
Replace ΒΆ
-
Text replacement:
$ sed 's/string to replace/replacement string/g' original-file.txt > new-file.txt
-
Dynamic, in-place replacement of
unreleased
text with todayβs date:$ sed -i "s/unreleased/`date +'%Y-%m-%d'`/" ./changelog.md
-
Update the release date in
citation.cff
file:$ perl -pi -e "s/date-released: \d+-\d+-\d+/date-released: $(date +'%Y-%m-%d')/" ./citation.cff
-
Replace all occurrences of
str1
bystr2
in all files below the/folder
path:$ find /folder -type f -print -exec sed -i 's/str1/str2/g' "{}" \;
-
Same as above but ignore all content of
.svn
folders and.zip
files:$ find /folder -type f -not -regex ".*\/\.svn\/.*" -not -iname "*\.zip" -print -exec sed -i 's/str1/str2/g' "{}" \;
-
Remove trailing spaces and tabs in every XML files:
$ find /folder -iname "*.xml" -exec sed -i 's/[ \t]*$//' "{}" \;
-
Place a new
---
line at the start of each.markdown
files ( see result ):$ find ./folder -iname "*.markdown" -exec sed -i '1s/^/---\n/' "{}" \;
-
Place a new
---
line before the first empty line of each.markdown
files ( see result ):$ find ./folder -iname "*.markdown" -exec sed -i '0,/^$/s//---\n/' "{}" \;
-
Remove lines starting with
prefix1:
orprefix2:
in all.markdown
files:$ find /folder -iname "*.markdown" -exec perl -p -i -e 's/(prefix1|prefix2): .*\n//sg' "{}" \;
-
Strip time from
date:
prefixed lines and quote the value. I.e. replace all occurrences ofdate: 2012-01-01 00:00:00 +0000
bydate: "2012-01-01"
:$ find /folder -iname "*.markdown" -exec perl -p -i -e 's/date: (\d+-\d+-\d+) .*\n/date: "$1"\n/sg' "{}" \;
-
Remove lines matching a regex (encoding particular markdown TOC entries ), save the result in place and save a backup of the original content in a
.bak
file:$ gawk -i inplace -v INPLACE_SUFFIX=.bak '!/^- \[(Contribute|Contributing|Licence|License)\]\(#.+\)$/{print}' ./readme.md
-
Use
sed
address ranges to spot, in a Markdown file, all blocks led by a:::
directive, and terminated by a blank line. Then replace in each of these matched blocks thea
letter byXXX
. Notice howa
occurrences outside the blocks are not replaced byXXX
:$ cat ./example.md This is a code block: :::shell-session β apache β java β python This is another block: :::shell-session β rust β haskell β javascript This is a random sentence. $ sed "/^:::/,/^$/ s/a/XXX/g" ./example.md This is a code block: :::shell-session β XXXpXXXche β jXXXvXXX β python This is another block: :::shell-session β rust β hXXXskell β jXXXvXXXscript This is a random sentence.
-
In the same spirit as above but this time to spot indented blocks starting with
:::
, then wrap them into triple-backticks fences:$ cat ./example.md This is a code block: :::shell-session β apache β java β python This is another block: :::shell-session β rust β haskell β javascript This is a random sentence. $ find ./folder -iname "*.md" \ > -exec sed -i "/^ :::/,/^$/ s/^$/ \`\`\`\n/" "{}" \; \ > -exec sed -i "/^ :::/,/^$/ s/:::/\`\`\`/" "{}" \; $ cat ./example.md This is a code block: ```shell-session β apache β java β python ``` This is another block: ```shell-session β rust β haskell β javascript ``` This is a random sentence.
-
Strip in-place the block of text starting with
XXX
and ending with an empty line:$ cat ./example.md This is a code block: XXX{shell-session} β apache β java β python This is a random sentence. $ perl -i -ne "print if not /XXX/ .. /^$/" ./example.md $ cat ./example.md This is a code block: This is a random sentence.
-
Same as above, but with
sed
:$ sed -i "/^XXX/,/^$/ d" ./example.md
-
Python one-liner to delete the first occurrence of a block of text delimited by triple-backticks fences. Contrary to methods above, this one is not distracted by blank lines within the text block:
$ python -c 'import re; from pathlib import Path; file = Path("./example.md"); file.write_text(re.sub(r"^\`\`\`.*?\`\`\`\n\n", "", file.read_text(), count=1, flags=re.MULTILINE | re.DOTALL))'
-
Append the content of the
addendum.txt
file to all.markdown
files:$ find ./folder -iname "*.markdown" -print -exec bash -c 'cat ./addendum.txt >> "{}"' \;
-
Replace all accentuated characters by their non-accentuated variants (thanks Matthieu for the tip):
$ echo "Γ©ΓΓ§a-$" | iconv -t ASCII//translit
Date & Time ΒΆ
-
Get the date of last week:
$ date +"%Y-%m-%d" -d last-week
-
Get the current date in english:
$ env LC_TIME=en date +"%a %b %d %Y"
-
Get the number of seconds since epoch :
$ date +%s
-
Convert back epoch time to human-readable date:
$ date --date=@1234567890
Transcoding ΒΆ
-
In place charset transcoding:
$ recode utf-8..latin-1 utf8text.txt
Edition ΒΆ
-
VIM: no autoindent on paste .
-
Get rid of Non-Breaking space on Linux systems by the way X.orgβs
~/.xmodmap
config file.