1

说,我们有一个代码:

xidel -s https://www.example.com -e '(//span[@class="number"])'

输出是:

111111
222222
333333

我可以在下面做这个吗?

for ((n=1;n<=3;n++))
do
   a=$(xidel -s https://www.example.com -e '(//span[@class="number"]) [$n]')
   b=$a+1
   echo $b
done

我希望它打印出 3 个编辑过的数字,如下所示:

111112
222223
333334

将网页下载 3 次可能有点荒谬,但这里的原因是使用 ForLoop 将输出的每个值一个一个处理。

4

2 回答 2

1

示例代码:

$ aa=$(xidel -se '//span[@class="random"]' 'https://www.example.com')
$ echo $aa

假设 xidel 的结果如下所示:

a abc
a sdf
a wef
a vda
a gdr

并且...假设我们想a在这种情况下从这个列表的每个单词中删除所有的,不限于仅排除a.

我们可以使用For Loop这样的公式:

#"a " is the one we want to remove, so make variable for this prefix
a="a "

for ((n=-1;n>=-5;n--))
do
 #process the extraction by selecting which line first
   bb=$(echo "$aa" | head $n | tail -1)
 #then remove the prefix after that
   bb=${aa/#$a}
   echo $bb

done

这将打印:

abc
sdf
wef
vda
gdr

奖金

#"a " is the one we want to remove, so make variable for this prefix
a="a "

for ((n=-1;n>=-5;n--))
do
 #process the extraction by selecting which line first
   bb=$(echo "$aa" | head $n | tail -1)
 #then remove the prefix after that
   bb=${aa/#$a}
 #echo everything except 2nd line
 if [ $n != -2 ] ; then
 echo $bb
 fi

done

这将打印:

abc
wef
vda
gdr

欢迎任何其他输入

于 2020-06-16T07:13:30.160 回答
1

xidel完全支持 XPath/XQuery 3.0(对 XPath/XQuery 3.1 的支持正在开发中),因此您可以使用它提供的所有功能和过滤器。
我可以推荐以下网站:


如果没有“最小的、可重现的示例”,我只会将您上面提到的输出按顺序排列并展示一些示例。

xidel -se 'let $a:=(111111,222222,333333) return $a ! (. + 1)'
#or
xidel -se 'for $x in (111111,222222,333333) return $x + 1'
111112
222223
333334
xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! substring-after(.,"a ")'
#or
xidel -se 'let $a:=("a abc","a sdf","a wef","a vda","a gdr") return $a ! replace(.,"a ","")'
#or
xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return substring-after($x,"a ")'
#or
xidel -se 'for $x in ("a abc","a sdf","a wef","a vda","a gdr") return replace($x,"a ","")'
abc
sdf
wef
vda
gdr
于 2020-06-16T22:31:05.177 回答