What is a script to find lines in a text starting with a particular string and prepend # to those lines?

StackOverflow https://stackoverflow.com/questions/19024038

  •  29-06-2022
  •  | 
  •  

質問

I am looking for an AppleScript or shell script (or another script) that automates the prepending of # before each line that starts with a date formatted like this one: 2013-09-26 (or any other date)

I keep a log, but want to make headers using Markdown. Doing that manually for each of the notes I have is a bit of a hassle.

役に立ちましたか?

解決

Using sed:

sed -r 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}/#&/' inputfile

Example:

$ cat in
foo
2012-01-20
bar
$ sed -r 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}/#&/' in
foo
#2012-01-20
bar

EDIT: As pointed out by user495470, on MacOS you'd need to use -E instead of -r in order to enable extended regular expressions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top