Search
-
Count the number of lines with at least one occurrence of the
ycharacter:1$ cat test.txt 2asd dd :; > 3y YYYyy yyy 4 . 5 6asdkjlyes 7 kjkjhkjhy 8 9$ grep -o '.*y.*' ./test.txt | wc -l 103
Replace
-
Text replacement:
1$ sed 's/string to replace/replacement string/g' original-file.txt > new-file.txt
-
Dynamic, in-place replacement of
unreleasedtext with todayβs date:1$ sed -i "s/unreleased/`date +'%Y-%m-%d'`/" ./changelog.md
-
Update the release date in
citation.cfffile:1$ perl -pi -e "s/date-released: \d+-\d+-\d+/date-released: $(date +'%Y-%m-%d')/" ./citation.cff
-
Replace all occurrences of
str1bystr2in all files below the/folderpath:1$ find /folder -type f -print -exec sed -i 's/str1/str2/g' "{}" \;
-
Same as above but ignore all content of
.svnfolders and.zipfiles:1$ 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:
1$ find /folder -iname "*.xml" -exec sed -i 's/[ \t]*$//' "{}" \;
-
Place a new
---line at the start of each.markdownfiles ( see result ):1$ find ./folder -iname "*.markdown" -exec sed -i '1s/^/---\n/' "{}" \;
-
Place a new
---line before the first empty line of each.markdownfiles ( see result ):1$ find ./folder -iname "*.markdown" -exec sed -i '0,/^$/s//---\n/' "{}" \;
-
Remove lines starting with
prefix1:orprefix2:in all.markdownfiles:1$ 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 +0000bydate: "2012-01-01":1$ find /folder -iname "*.markdown" -exec perl -p -i -e 's/date: (\d+-\d+-\d+) .*\n/date: "$1"\n/sg' "{}" \;
-
Remove in all
.markdownfiles all occurrences of Amazonβs pixel tracking links in the form of:1$ find ./* -iname "*.md" -exec perl -0777 -i -pe "s/\!\[\]\(https:\/\/www\.assoc-amazon\.com.*?\)//gs" "{}" \;
-
Shorten in all
.markdownfiles Amazonβ product URLs from(https://www.amazon.com/dp/B00F3F0GLU/?tag=accounid-20)to(https://amzn.com/B00F3F0GLU/?tag=accounid-20):1$ find ./* -iname "*.md" -exec perl -0777 -i -pe "s/\(https:\/\/www\.amazon\.com\/dp\/(.*?)\)/\(https:\/\/amzn\.com\/\1\)/gs" "{}" \;
-
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
.bakfile:1$ gawk -i inplace -v INPLACE_SUFFIX=.bak '!/^- \[(Contribute|Contributing|Licence|License)\]\(#.+\)$/{print}' ./readme.md
-
Use
sedaddress 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 thealetter byXXX. Notice howaoccurrences 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
XXXand 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:1$ 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:
1$ 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.txtfile to all.markdownfiles:1$ 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):
1$ echo "Γ©ΓΓ§a-$" | iconv -t ASCII//translit
Date & Time
-
Get the date of last week:
1$ date +"%Y-%m-%d" -d last-week
-
Get the current date in english:
1$ env LC_TIME=en date +"%a %b %d %Y"
-
Get the number of seconds since epoch :
1$ date +%s
-
Convert back epoch time to human-readable date:
1$ date --date=@1234567890
Transcoding
-
In place charset transcoding:
1$ 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
~/.xmodmapconfig file.