Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

awk - Search through a markdown using sed

The Problem

I multiple Markdown files in a folder, formatted like so...

# Cool Project
* Random Text 
* Other information
TODO: This is a task
TODO: This is another task

And I've written a script that pulls out all the strings that start with TODO from all the files...

 ag TODO: ~/myfolder/journal | sed  's/(^.*:)(.*)/TODO:2 /g' | sed ''/TODO:/s//`printf "33[35mTODO:33[0m"`/'' | sed ''s/![a-zA-Z0-9]*/$(printf "33[31;1m&33[0m")/''

and this gives me an output like this

TODO: This is a task
TODO: This is another task

I was wondering if it would be possible to look backward from the pattern using sed to identify and pickup the line that starts with /^# / and appended it to the end of the string... something like this

TODO: This is a task # Cool Project
TODO: This is another task # Cool Project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using sed:

sed -n '/^#/h;/^TODO/{G;s/
/ /p}' file

Search for lines beginning with # and add to hold space (h) Then when a line begins with "TODO", append hold space to pattern space (G) and substitute new lines for a space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...