1

below content has been written in a text file called test.txt. How can I retrieve pending & completed count value in shell script?

<p class="pending">Count: 0</p>
<p class="completed">Count: 0</p>

Here's what I tried:

#!/bin/bash

echo
echo 'Fetching job page and write to Jobs.txt file...'
curl -o Jobs.txt https://cms.test.com

completestatus=`grep "completed" /home/Jobs.txt | awk -F "<p|< p="">" '{print $2 }' | awk '{print $4 }'`
echo $completestatus
if [ "$completestatus" == 0 ]; then
2
  • how to get the completed count?.curl -o Jobs.txt test.cmx.com status=`grep "completed" /home/Jobs.txt Commented Oct 4, 2016 at 12:36
  • You call it a 'text' file, but it's trivial to wrap it up as XML, then you'd have a whole suite of more appropriate tools for this sort of query. Don't be tempted to transform it into to a shell script and eval that - it's a recipe for a security vulnerability. Commented Oct 4, 2016 at 14:56

3 Answers 3

1

grep and awk commands can almost always be combined into 1 awk command. And 2 awk commands can almost always be combined to 1 awk command also.

This solves your immediate problem (using a little awk type casting trickery).

completedStatus=$(echo "<p class="pending">Count: 0</p>^J
      <p class="completed">Count: 0</p>" \
    | awk -F : '/completed/{var=$2+0.0;print var}' )
echo completedStatus=$completedStatus

The output is

completedStatus=0

Note that you can combine grep and awk with

awk -F : '/completed/' test.txt

filters to just the completed line , output

<p class=completed>Count: 0</p>

When I added your -F argument, the output didn't change, i.e.

  awk -F'<p|< p="">' '/completed/' test.txt

output

<p class=completed>Count: 0</p>

So I relied on using : as the -F (field separator). Now the output is

awk -F : '/completed/{print $2}'

output

 0</p>

When performing a calculation, awk will read a value "looking" for a number at the front, if it finds a number, it will read the data until it finds a non-numeric (or if there is nothing left). So ...

  awk -F : '/completed/{var=$2+0.0;print var}' test.txt

output

  0

Finally we arrive at the solution above, wrap the code in a modern command-substitution, i.e. $( ... cmds ....) and send the output to the completedStatus= assignment.


In case you're thinking that the +0.0 addition is what is being output, you can change your file to show completed count = 10, and the output will be 10.

IHTH

Sign up to request clarification or add additional context in comments.

Comments

1

another awk

completedStatus=$(awk -F'[ :<]' '/completed/{print $(NF-1)}' file)

Comments

0

If I got you right, you just want to extract pending or completed and the value. If that is the case,

Then Using SED,

please check out below script.Output shared via picture, please click to see

#!/bin/bash

file="$1"

echo "Simple"

cat $1 |sed 's/^.*=\"\([a-z]*\)\">Count: \([0-9]\)<.*$/\1=\2/g'

echo "Pipe Separated"

cat $1 |sed 's/^.*=\"\([a-z]*\)\">Count: \([0-9]\)<.*$/\1|\2/g'

echo "CSV Style or comma separeted"

cat $1 |sed 's/^.*=\"\([a-z]*\)\">Count: \([0-9]\)<.*$/\1,\2/g'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.