副标题:“我是来学习的。”
我需要从Markdown到另一种格式的几个链接集合(具体来说,通过这个剧本将文件中的每一行作为一个便条发送到Evernote )。
我的示例文件都如下所示:
* [REF_1](URL)
* [REF_2](URL)
* [REF_3](URL)编辑:或更准确地说:
* [Koken Installtion Help](http://help.koken.me/customer/portal/articles/632102-installation)
* [A couple of Sass functions](http://hugogiraudel.com/2013/08/12/sass-functions/)
* [Chris Coyier's Favorite CodePen Demos](http://davidwalsh.name/chris-coyiers-favorite-pens)…这是我的战斗计划:
^[\*\-\+]\s折叠[REF]的内容(方括号中的所有内容)与正则表达式(?<=\[)(.*)(?=\])匹配,并将其设置为变量${titel}[URL]的内容(括号之间的所有内容)与正则表达式(?<=\()(.*)(?=\))匹配,并将其设置为变量${url}markdown2evernote.rb示例(参见第5点):
Notebook: ${notebook}
Title: ${title}
Url: ${url}
Keywords: ${keywords}
Content Area
Will use the $title and $url variable here again.坦率地说,我还没走多远。我已经被困在第二点了,因为我想不出怎样才能让正则表达式起作用。现在,我的bash脚本如下所示:
#!/bin/sh
document=~/file.txt
notebook="My test notebook"
keywords="test, demo"
cat ${document} | while read line; do \
echo "$line" | sed -e '(?<=\[)(.*)(?=\])'; \
done我不在乎它是否是sed/grep/awk解决方案--只要它是shell脚本,我就都接受它。
我还会感兴趣的是,在检查列表中的第2点和第3点之后,最好的方法是什么,并达到我的目标,即有一个格式化的输出,可以交给ruby脚本。
如上所述,我有一个模糊的想法,但这是一个相当大的挑战,因为到目前为止,我编写的脚本要简单得多,而且不涉及上面的任何内容。
发布于 2013-10-30 20:49:29
您可以使用sed提取变量并写入输出。
sed 's/^[*-+] *\[\(.*\)\](\(.*\))/Notebook: x\nTitle: \1\nUrl: \2\nKeywords: y\n\nContent Area\n\nWill use the \1 and \2 variable here again/' file.txt通过使用\(和\),您可以使用\1、\2等访问这些括号中的模式。
但是,如果您的输出包含大量文本,那么您将只使用文本来破坏sed命令,从而降低它的可读性。数据和程序控制结构应该分开,所以我建议如下:
#!/usr/bin/env bash
fillText(){
echo "Notebook: ${1}"
echo "Title: ${2}"
echo "Url: ${3}"
echo "Keywords: ${4}"
echo
echo "Content Area"
echo
echo "Will use ${2} and ${3} variable here again."
}
document=file
notebook="My test notebook"
keywords="test, demo"
while read line; do
title=$(sed 's/^[*-+] *\[\(.*\)\](.*)/\1/' <<< "${line}")
url=$(sed 's/^[*-+] *\[.*\](\(.*\))/\1/' <<< "${line}")
fillText "${notebook}" "${title}" "${url}" "${keywords}"
done < "${document}"函数fillText()以您想要的方式编写输出,它使用四个位置参数,其中两个是从带有sed的"${document}"中提取的。
fillText函数也可以在不同的文件中定义。
顺便提一下,您的代码的某些部分有一个注释:
cat ${document} | while read line; do \
echo "$line" | sed -e '(?<=\[)(.*)(?=\])'; \
done完全相同
sed '(?<=\[)(.*)(?=\])' "${document}"(无视这一点,我不知道'(?<=\[)(.*)(?=\])'是什么意思。
https://stackoverflow.com/questions/19693186
复制相似问题