sed命令在ruby中不起作用,但在terminal中起作用
当我在ruby脚本中运行这行sed时,我遇到了一个问题:sed命令在ruby中不起作用,但在terminal中起作用,ruby,bash,sed,terminal,Ruby,Bash,Sed,Terminal,当我在ruby脚本中运行这行sed时,我遇到了一个问题: sed -ne '/^<div class="results">/,/<\/body>/p' blah.html sed-ne'/^/,//p'blah.html 我试图在backticks``中运行它,但它无法在Ruby脚本中正确执行。blah.html的内容如下所示 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC
sed -ne '/^<div class="results">/,/<\/body>/p' blah.html
sed-ne'/^/,//p'blah.html
我试图在backticks``中运行它,但它无法在Ruby脚本中正确执行。blah.html的内容如下所示
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>RSpec results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Pragma" content="no-cache" />
<style type="text/css">
body {
margin: 0;
padding: 0;
background: #fff;
font-size: 80%;
}
</style>
<script type="text/javascript">
// <![CDATA[
function addClass(element_id, classname) {
document.getElementById(element_id).className += (" " + classname);
}
function removeClass(element_id, classname) {
var elem = document.getElementById(element_id);
var classlist = elem.className.replace(classname,'');
elem.className = classlist;
}
function moveProgressBar(percentDone) {
document.getElementById("rspec-header").style.width = percentDone +"%";
}
function makeRed(element_id) {
removeClass(element_id, 'passed');
removeClass(element_id, 'not_implemented');
addClass(element_id,'failed');
}
function makeYellow(element_id) {
var elem = document.getElementById(element_id);
if (elem.className.indexOf("failed") == -1) { // class doesn't includes failed
if (elem.className.indexOf("not_implemented") == -1) { // class doesn't include not_implemented
removeClass(element_id, 'passed');
addClass(element_id,'not_implemented');
}
}
}
function apply_filters() {
var passed_filter = document.getElementById('passed_checkbox').checked;
var failed_filter = document.getElementById('failed_checkbox').checked;
var pending_filter = document.getElementById('pending_checkbox').checked;
assign_display_style("example passed", passed_filter);
assign_display_style("example failed", failed_filter);
assign_display_style("example not_implemented", pending_filter);
assign_display_style_for_group("example_group passed", passed_filter);
assign_display_style_for_group("example_group not_implemented", pending_filter, pending_filter || passed_filter);
assign_display_style_for_group("example_group failed", failed_filter, failed_filter || pending_filter || passed_filter);
}
function get_display_style(display_flag) {
var style_mode = 'none';
if (display_flag == true) {
style_mode = 'block';
}
return style_mode;
}
function assign_display_style(classname, display_flag) {
var style_mode = get_display_style(display_flag);
var elems = document.getElementsByClassName(classname)
for (var i=0; i<elems.length;i++) {
elems[i].style.display = style_mode;
}
}
function assign_display_style_for_group(classname, display_flag, subgroup_flag) {
var display_style_mode = get_display_style(display_flag);
var subgroup_style_mode = get_display_style(subgroup_flag);
var elems = document.getElementsByClassName(classname)
for (var i=0; i<elems.length;i++) {
var style_mode = display_style_mode;
if ((display_flag != subgroup_flag) && (elems[i].getElementsByTagName('dt')[0].innerHTML.indexOf(", ") != -1)) {
elems[i].style.display = subgroup_style_mode;
} else {
elems[i].style.display = display_style_mode;
}
}
}
// ]]>
</script>
<style type="text/css">
#rspec-header {
background: #65C400; color: #fff; height: 4em;
}
.rspec-report h1 {
margin: 0px 10px 0px 10px;
padding: 10px;
font-family: "Lucida Grande", Helvetica, sans-serif;
font-size: 1.8em;
position: absolute;
}
#label {
float:left;
}
#display-filters {
float:left;
padding: 28px 0 0 40%;
font-family: "Lucida Grande", Helvetica, sans-serif;
}
#summary {
float:right;
padding: 5px 10px;
font-family: "Lucida Grande", Helvetica, sans-serif;
text-align: right;
}
#summary p {
margin: 0 0 0 2px;
}
#summary #totals {
font-size: 1.2em;
}
.example_group {
margin: 0 10px 5px;
background: #fff;
}
dl {
margin: 0; padding: 0 0 5px;
font: normal 11px "Lucida Grande", Helvetica, sans-serif;
}
dt {
padding: 3px;
background: #65C400;
color: #fff;
font-weight: bold;
}
dd {
margin: 5px 0 5px 5px;
padding: 3px 3px 3px 18px;
}
dd .duration {
padding-left: 5px;
text-align: right;
right: 0px;
float:right;
}
dd.example.passed {
border-left: 5px solid #65C400;
border-bottom: 1px solid #65C400;
background: #DBFFB4; color: #3D7700;
}
dd.example.not_implemented {
border-left: 5px solid #FAF834;
border-bottom: 1px solid #FAF834;
background: #FCFB98; color: #131313;
}
dd.example.pending_fixed {
border-left: 5px solid #0000C2;
border-bottom: 1px solid #0000C2;
color: #0000C2; background: #D3FBFF;
}
dd.example.failed {
border-left: 5px solid #C20000;
border-bottom: 1px solid #C20000;
color: #C20000; background: #FFFBD3;
}
dt.not_implemented {
color: #000000; background: #FAF834;
}
dt.pending_fixed {
color: #FFFFFF; background: #C40D0D;
}
dt.failed {
color: #FFFFFF; background: #C40D0D;
}
#rspec-header.not_implemented {
color: #000000; background: #FAF834;
}
#rspec-header.pending_fixed {
color: #FFFFFF; background: #C40D0D;
}
#rspec-header.failed {
color: #FFFFFF; background: #C40D0D;
}
.backtrace {
color: #000;
font-size: 12px;
}
a {
color: #BE5C00;
}
/* Ruby code, style similar to vibrant ink */
.ruby {
font-size: 12px;
font-family: monospace;
color: white;
background-color: black;
padding: 0.1em 0 0.2em 0;
}
.ruby .keyword { color: #FF6600; }
.ruby .constant { color: #339999; }
.ruby .attribute { color: white; }
.ruby .global { color: white; }
.ruby .module { color: white; }
.ruby .class { color: white; }
.ruby .string { color: #66FF00; }
.ruby .ident { color: white; }
.ruby .method { color: #FFCC00; }
.ruby .number { color: white; }
.ruby .char { color: white; }
.ruby .comment { color: #9933CC; }
.ruby .symbol { color: white; }
.ruby .regex { color: #44B4CC; }
.ruby .punct { color: white; }
.ruby .escape { color: white; }
.ruby .interp { color: white; }
.ruby .expr { color: white; }
.ruby .offending { background-color: gray; }
.ruby .linenum {
width: 75px;
padding: 0.1em 1em 0.2em 0;
color: #000000;
background-color: #FFFBD3;
}
</style>
</head>
<body>
<div class="rspec-report">
<div id="rspec-header">
<div id="label">
<h1>RSpec Code Examples</h1>
</div>
<div id="display-filters">
<input id="passed_checkbox" name="passed_checkbox" type="checkbox" checked onchange="apply_filters()" value="1"> <label for="passed_checkbox">Passed</label>
<input id="failed_checkbox" name="failed_checkbox" type="checkbox" checked onchange="apply_filters()" value="2"> <label for="failed_checkbox">Failed</label>
<input id="pending_checkbox" name="pending_checkbox" type="checkbox" checked onchange="apply_filters()" value="3"> <label for="pending_checkbox">Pending</label>
</div>
<div id="summary">
<p id="totals"> </p>
<p id="duration"> </p>
</div>
</div>
<div class="results">
<div id="div_group_1" class="example_group passed">
<dl style="margin-left: 0px;">
<dt id="example_group_1" class="passed">Ark - Header (styles)</dt>
</dl>
</div>
<div id="div_group_2" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_2" class="passed">that we have hit a valid URL</dt>
<script type="text/javascript">moveProgressBar('14.2');</script>
<dd class="example passed"><span class="passed_spec_name">should not return an invalid error message</span><span class='duration'>7.76052s</span></dd>
</dl>
</div>
<div id="div_group_3" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_3" class="passed">Ark Home Button</dt>
<script type="text/javascript">moveProgressBar('28.5');</script>
<dd class="example passed"><span class="passed_spec_name">should exist</span><span class='duration'>2.22255s</span></dd>
</dl>
</div>
<div id="div_group_4" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_4" class="passed">Ark Sign in link</dt>
<script type="text/javascript">moveProgressBar('42.8');</script>
<dd class="example passed"><span class="passed_spec_name">should exist</span><span class='duration'>1.68413s</span></dd>
</dl>
</div>
<div id="div_group_5" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_5" class="passed">Ark Home Button Style</dt>
<script type="text/javascript">moveProgressBar('57.1');</script>
<dd class="example passed"><span class="passed_spec_name">should have accurate styling</span><span class='duration'>2.98297s</span></dd>
</dl>
</div>
<div id="div_group_6" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_6" class="passed">Ark SignIn Button Style</dt>
<script type="text/javascript">moveProgressBar('71.4');</script>
<dd class="example passed"><span class="passed_spec_name">should have accurate styling</span><span class='duration'>3.77360s</span></dd>
</dl>
</div>
<div id="div_group_7" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_7" class="passed">Ark header Topbar Style</dt>
<script type="text/javascript">moveProgressBar('85.7');</script>
<dd class="example passed"><span class="passed_spec_name">should have accurate styling</span><span class='duration'>2.78883s</span></dd>
</dl>
</div>
<div id="div_group_8" class="example_group passed">
<dl style="margin-left: 15px;">
<dt id="example_group_8" class="passed">Post Sign-In Style</dt>
<script type="text/javascript">moveProgressBar('100.0');</script>
<dd class="example passed"><span class="passed_spec_name">should have accurate styling</span><span class='duration'>17.62036s</span></dd>
</dl>
</div>
<script type="text/javascript">document.getElementById('duration').innerHTML = "Finished in <strong>38.83569 seconds</strong>";</script>
<script type="text/javascript">document.getElementById('totals').innerHTML = "7 examples, 0 failures";</script>
</div>
</div>
</body>
</html>
RSpec结果
身体{
保证金:0;
填充:0;
背景:#fff;
字号:80%;
}
//
#rspec头{
背景:65C400;颜色:fff;高度:4em;
}
.rspec报告h1{
保证金:0px 10px 0px 10px;
填充:10px;
字体系列:“Lucida Grande”,Helvetica,无衬线;
字号:1.8em;
位置:绝对位置;
}
#标签{
浮动:左;
}
#显示过滤器{
浮动:左;
填充:28px040%;
字体系列:“Lucida Grande”,Helvetica,无衬线;
}
#总结{
浮动:对;
填充物:5px10px;
字体系列:“Lucida Grande”,Helvetica,无衬线;
文本对齐:右对齐;
}
#摘要p{
边际:0.2px;
}
#汇总#总计{
字体大小:1.2米;
}
.例如{
利润率:0 10px 5px;
背景:#fff;
}
dl{
边距:0;填充:0 5px;
字体:普通11px“Lucida Grande”,Helvetica,无衬线;
}
dt{
填充:3倍;
背景:#65C400 ;;
颜色:#fff;
字体大小:粗体;
}
dd{
保证金:5px 0 5px 5px;
填充:3px 3px 3px 18px;
}
dd.持续时间{
左侧填充:5px;
文本对齐:右对齐;
右:0px;
浮动:对;
}
dd.example.passed{
左边框:5px实心#65C400;
边框底部:1px实心#65C400;
背景:DBFFB4;颜色:3D7700;
}
dd.example.not_实现{
左边框:5px实心#FAF834;
边框底部:1px实心#FAF834;
背景:FCFB98;颜色:131313;
}
dd.example.pending_已修复{
左边框:5px实心#0000C2;
边框底部:1px实心#0000C2;
颜色:#0000C2;背景:#D3FBFF;
}
dd.example.failed{
左边框:5px实心#C20000;
边框底部:1px实心#C20000;
颜色:#C20000;背景:#FFFBD3;
}
dt.未实现{
颜色:#000000;背景:#FAF834;
}
dt.pending_固定{
颜色:#FFFFFF;背景:#C40D0D;
}
dt失败了{
颜色:#FFFFFF;背景:#C40D0D;
}
#未实现rspec-header.U{
颜色:#000000;背景:#FAF834;
}
#rspec-header.PENG_已修复{
颜色:#FFFFFF;背景:#C40D0D;
}
#rspec-header.0失败{
颜色:#FFFFFF;背景:#C40D0D;
}
.回溯{
颜色:#000;
字体大小:12px;
}
a{
颜色:#BE5C00;
}
/*Ruby代码,风格类似于vibrant ink*/
露比先生{
字体大小:12px;
字体系列:monospace;
颜色:白色;
背景色:黑色;
填充:0.1米0.2米0;
}
.ruby.keyword{color:#FF6600;}
.ruby.constant{color:#339999;}
.ruby.attribute{color:white;}
.ruby.global{color:white;}
.ruby.module{color:white;}
.ruby.class{color:white;}
.ruby.string{color:#66FF00;}
.ruby.ident{color:white;}
.ruby.method{color:#FFCC00;}
.ruby.number{color:white;}
.ruby.char{color:white;}
.ruby.comment{color:#9933CC;}
.ruby.symbol{color:white;}
.ruby.regex{color:#44B4CC;}
.ruby.punct{颜色:白色;}
.ruby.escape{color:white;}
.ruby.interp{color:white;}
.ruby.expr{color:white;}
.ruby.disking{背景色:灰色;}
.ruby.linenum{
宽度:75px;
填充:0.1米1米0.2米0;
颜色:#000000;
背景色:#FFFBD3;
}
RSpec代码示例
通过
失败
悬而未决的
方舟头(样式)
我们找到了一个有效的URL
moveProgressBar('14.2');
不应返回无效的错误消息7.76052s
方舟主页按钮
moveProgressBar('28.5');
应该存在2.2225s
方舟登录链接
moveProgressBar('42.8');
应该存在1.68413s
方舟主按钮样式
moveProgressBar('57.1');
应具有准确的样式2.98297s
方舟标志钮扣样式
moveProgressBar('71.4');
应具有准确的样式3.77360s
方舟头顶杆式
moveProgressBar('85.7');
应具有准确的样式2.78883s
后登录样式
moveProgressBar('100.0');
应具有准确的样式17.62036s
document.getElementById('duration').innerHTML=“在38.83569秒内完成”;
document.getElementById('totals').innerHTML=“7个示例,0个失败”;
当我在MacOSX中从终端尝试相同的命令时,它可以正常工作
这是我在尝试从Ruby中运行命令时遇到的错误:
sed: 2: "/^<div class="results"> ...": undefined label 'ody>/p'
sed:2:“/^…”:未定义的标签“ody>/p”
在执行命令之前,Ruby将用一个简单的/
替换\/
序列。所以你的sed脚本毁了。请尝试使用双反斜杠(\/
)。在执行命令之前,Ruby将用一个简单的\/
序列替换\/
。所以你的sed脚本毁了。请尝试使用双反斜杠(\\/
)。谢谢!这就解决了!!非常感谢!谢谢这就解决了!!非常感谢!