【问题标题】:How to parse jquery using bash or perl如何使用 bash 或 perl 解析 jquery
【发布时间】:2014-01-04 16:26:45
【问题描述】:

我正在尝试像这样解析 jquery 代码:(这是主要部分,但不是全部)

$("#column-wide").css({"background-color":"#eff6f9"});
$("#2626").css({"width":"300px"});
$("#2627").css({"width":"300px"});
$("#2628").css({"width":"300px"});
$("#2629").css({"width":"300px"});
$(".Country > label").css({"position":"relative", "left":"-20px", "top":0});
$("#2631").css({"position":"relative", "left":"-20px", "top":0, "width":128,   "height":29});
$("#2633").css({"width":"300px"});
$("#2634").css({"width":"300px"});
// CONTACT INFO BELOW: 
$("#2633").css({});
$("#2633").css({"position":"relative", "left":0, "top":0, "width":289, "height":19});
$("#2630").css({"position":"relative", "left":0, "top":0, "width":46, "height":19});
$("#2638").css({"width":"300px"});
$("#2639").css({"width":"300px"});
$(".Prefix").css({"display":"none", "visibility":""});
$(".Last_Name").css({"left":39, "top":3});
$("#2636").css({"position":"relative", "left":49, "top":1});
$("#2640").css({"position":"relative", "left":0, "top":0});
$("#2640").css({"width":"88px"});
$(".State > label").css({"position":"relative", "left":"-60px", "top":0});
$("#2641").css({"position":"relative", "left":"-116px", "top":0});
$("#2641").css({"width":"88px"});
$(".State > label").css({"left":"-121px"});
$("#2641").css({"left":"-161px", "width":98, "height":29});
$("#2637").css({"position":"relative", "left":"5px", "top":0});
$(".State > label").css({"left":"-109px"});
$("#2642").css({"width":"88px"});
$("#2643").css({"width":"88px"});
$(".Alternate_Phone").css({"left":"-129px"});
$(".State > label").css({"left":"-81px"});
$("#2643").css({"position":"relative", "left":"-32px", "top":0});
$(".UserAgreement label").css({"position":"relative", "left":0, "top":0, "width":419,         "height":77});

像这样进入一行,只选择带有 .css 的行,没有别的:

$('head').append("<style> #2626 { width: 300px; } #2627 { width: 300px; } #2626 { width: 250px; height: 50px; } </style>");

我需要能够获取第一个 # 元素,所有带有 .css 的行,然后是变体,例如"width: 300px" 并将其放在如上所示的单行格式中。 我一直在尝试使用 bash 来做到这一点。

#!/bin/bash

echo Enter filename:
read fname

echo "#######"
while read line;do

echo -n "\$('head').append(\"<style> "
awk -F "[)(]" '/.css/{printf "%s %s ",$2,$4}' $fname|sed 's/\"//g;s/,/;/g'
echo -n  "</style>\");"

done < $fname
echo "Total number of lines in file: $k"

【问题讨论】:

  • 如果您在标题中说要使用 bash 或 perl,为什么这会被标记为“python”?
  • 使用 python 也是可以接受的 - 很抱歉造成混乱!
  • 它们来自哪里:#2626 { width: 250px;高度:50px; }

标签: jquery python parsing sed awk


【解决方案1】:
#! /usr/bin/bash
echo -n "\$('head').append(\"<style> "
awk -F "[)(]" '/.css/&&/#[0-9]+/{printf "%s %s ",$2,$4}' infile|sed 's/\"//g;s/,/;/g'
echo -n  "</style>\");"

结果:

$('head').append("<style> #2626 {width:300px} #2627 {width:300px} #2628 {width:300px} #2629 {width:300px} #2631 {position:relative; left:-20px; top:0; width:128;   height:29} #2633 {width:300px} #2634 {width:300px} #2633 {} #2633 {position:relative; left:0; top:0; width:289; height:19} #2630 {position:relative; left:0; top:0; width:46; height:19} #2638 {width:300px} #2639 {width:300px} #2636 {position:relative; left:49; top:1} #2640 {position:relative; left:0; top:0} #2640 {width:88px} #2641 {position:relative; left:-116px; top:0} #2641 {width:88px} #2641 {left:-161px; width:98; height:29} #2637 {position:relative; left:5px; top:0} #2642 {width:88px} #2643 {width:88px} #2643 {position:relative; left:-32px; top:0} </style>");

更新代码:

#! /usr/bin/bash
echo -n "\$('head').append(\"<style> "
awk -F "[)(]" '/.css/{printf "%s %s ",$2,$4}' infile|sed 's/\"//g;s/,/;/g'
echo -n  "</style>\");"

更新代码 #2:

#!/bin/bash

echo Enter filename:
read fname

echo "#######"

echo -n "\$('head').append(\"<style> "
awk -F "[)(]" '/.css/{printf "%s %s ",$2,$4}' $fname|sed 's/\"//g;s/,/;/g'
echo -n  "</style>\");"

【讨论】:

  • 我得到这个作为输出: $('head').append("");$ ('head').append("");$('head').append("");$('head').append("");$('head' ).append("
  • 把infile换成你的文件名,infile是我的测试文件名。
  • 我想我可能会让它适用于更简单的那些; #2626, #2627 并抓取它的操作 - 但是有多个字符串的操作呢? $("#column-wide").css({"background-color":"#eff6f9"}); $("#2626").css({"width":"300px"}); $("#2627").css({"width":"300px"}); $("#2628").css({"width":"300px"}); $("#2629").css({"width":"300px"}); $(".Country > label").css({"position":"relative", "left":"-20px", "top":0}); $("#2631").css({"position":"relative", "left":"-20px", "top":0, "width":128, "height":29});跨度>
  • 您需要提供所有可能的示例,并在原始问题中进行编辑。
  • 非常感谢您的帮助 BMW - 我已经添加了我需要解析的大部分文件 - 但它仍然不是全部。我将把它变成一个标准化的脚本,所以我希望它能够成为未来的证明,无论正在解析什么(尽管它始终是 .css)无论我是在抓取一个类还是 id “ #" 或 ".",或运算符的数量 "width:xx height:xx position, etc"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 2014-02-08
  • 2012-12-12
  • 2011-12-12
  • 1970-01-01
相关资源
最近更新 更多