CuriousNewbie
CuriousNewbie

Reputation: 329

Xidel: How to process multiple result from one node?

Say, we have a code:

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

and the output are:

111111
222222
333333

can I do this one below?

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

I expect it to print out 3 edited numbers like this:

111112
222223
333334

it might be a little absurb to download the webpage 3 times, but the reason here is to process each value of the output one by one, using ForLoop.

Upvotes: 1

Views: 636

Answers (2)

CuriousNewbie
CuriousNewbie

Reputation: 329

Example code:

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

Lets say that the result of xidel is this below:

a abc
a sdf
a wef
a vda
a gdr

and...lets say we want to cut all the a from each word of this list in this case, not limiting to just excluding the a.

We can use For Loop formula like this:

#"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

This will print:

abc
sdf
wef
vda
gdr

Bonus

#"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

This will print:

abc
wef
vda
gdr

Any other input is welcome

Upvotes: 1

Reino
Reino

Reputation: 3433

xidel fully supports XPath/XQuery 3.0 (support for XPath/XQuery 3.1 is in development), so you can use all the features and filters it has to offer.
I can recommend the following websites:


Without a "Minimal, Reproducible Example" I'll just put your above mentioned output in a sequence and show you some examples.

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

Upvotes: 1

Related Questions