I need to remove first three lines of multiple markdown files structured like that:
|- Folder
| - 01-07-22.md
| - 02-07-22.md
| - 03-07-22.md
| - ...
I would like to do this with Mac Terminal (if possible) because I have no expertise with any coding language and thus I don't have any coding platform installed on my computer.
Also I would like to know if other than deleting first 3 lines is possible to add "##" at the very beginning of every document.
What works:
- This work:
sed -i '' '1,3d' Folder/*.md
- The following command works also:
But it does not add a new line before the first line.sed -i '' '1i\ ##' *.md
- This does not work at all:
sed -i '' '1s /^/##/' *.md
How to add an empty line at the beginning and "##" at the beginning of the now second line? Explaination: From this:
# First line of example .md file
Second line of example .md
...
To this:
### First line of example .md file
Second line of example .md
...
CodePudding user response:
You should be able to use (GNU) sed
. I hope that the Mac version supports all the required flags and features.
To delete the first 3 lines:
sed -i '1,3d' Folder/*.md
To prepend the line ##
to every file:
sed -i '1i##' Folder/*.md
To prefix the existing first line with ##
:
sed -i '1s/^/##/' Folder/*.md
The original files are overwritten without confirmation, but you can specify -i.bak
to create backup files, e.g. Folder/01-07-22.md.bak
. Specify -i ''
to disable backup file creation.
Certain sed
implementations might always require an argument after -i
, so go with -i.bak
or -i .bak
.
If prepending a line does not work, try a different syntax (the newline is important):
sed -i .bak '1i\
##' Folder/*.md
If that doesn't work either, there's another form how sed could be invoked:
sed -i .bak -e '1i\' -e '##' Folder/*.md
If you want to modify the first line and add an empty line before it, e.g. transforming
1
2
3
into
##1
2
3
would require you to use the following command:
sed -i .bak '1s/^/##/;1i\
' Folder/*.md