LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   question about grep (https://www.linuxquestions.org/questions/linux-newbie-8/question-about-grep-4175450510/)

tpopono 02-17-2013 04:26 AM

question about grep
 
hello guys, im a starting out with linux and there is something i been stuck on

I been trying to display just the number of views from the youtube source code.

my professor gave us this command line to do it but for some reason i cant get it to work

Code:

grep data-item sourcecode.html | while read x : do views= expr "$x":".*data-content-item-views=\"\(.*\) views.*"
This is a sample line from the file
Code:

<div class="feed-item-content-wrapper clearfix context-data-item" data-context-item-time="24:39" data-context-item-actionuser="FUNimation" data-context-item-user="FUNimation" data-context-item-views="232,184 views" data-context-item-id="ff7N_X2Omkc" data-context-item-title="Fairy Tail - 18 - Reach the Sky Above (SUB)" data-context-item-type="video">
i just want to get the number of views, i dont know why is not working, can someone plz help me out?

Mr. Alex 02-17-2013 05:46 AM

Here’s a suggestion:

Code:

youtube-viewer -i 'http://www.youtube.com/watch?v=B89N78zeKAs' | grep Views | awk '{print $4}'
There is some output to clear out (like “Insert a valid code or search for videos (:h for help)”) but this will give you number of views.

shivaa 02-17-2013 07:04 AM

Try:
Code:

~$ awk '/data-context-item-views/ {print $8}' inputfile.html | awk -F"=" '{gsub(/\"/,"",$0); print $2}'

tpopono 02-17-2013 10:53 AM

Sorry
 
Thx for Ur help, but I forgot to mention that I can only use the Grep command , that's what the professors told us

shivaa 02-17-2013 11:48 AM

Quote:

Originally Posted by tpopono (Post 4893703)
Thx for Ur help, but I forgot to mention that I can only use the Grep command , that's what the professors told us

Command grep could help you to search some string or pattern in a file, but cannot cut specific field(s). In order to get some field (i.e. column) you will need other utilities like awk, cut or sed.

For more understanding, check manaul of grep here.

So using grep you can search the line having string views only.

whizje 02-17-2013 12:25 PM

Quote:

If it doesn't challenge you, it doesn't change you!
Code:

bash-4.2$ grep -A 1 "watch-view-count" watch.htm | grep -Eo '[0-9]*$'
196828

-A 1 = print also one line after a line with a match
"watch-view-count" = in the line before the count
watch.htm = is the html file from the youtube page you can also feed it with
Code:

bash-4.2$ cat watch.htm | grep -A 1 "watch-view-count" | grep -Eo '[0-9]*$'
196828

-E = use regular expressions
o = print only matching part of expression because before the number are spaces this is a way to get rid of them.
'[0-9]*$' = the regular expression [0-9] means character between 0 and 9 and the * means repeat the expression before * zero or more times, in this case look for each other following characters between 0 and 9. And the $ means the characters must be at the end of the line.

whizje 02-17-2013 12:40 PM

I must learn to first read the question
Code:

bash-4.2$ echo '<div class="feed-item-content-wrapper clearfix context-data-item" data-context-item-time="24:39" data-context-item-actionuser="FUNimation" data-context-item-user="FUNimation" data-context-item-views="232,184 views" data-context-item-id="ff7N_X2Omkc" data-context-item-title="Fairy Tail - 18 - Reach the Sky Above (SUB)" data-context-item-type="video">'| grep -Eo "[0-9,]* views" | grep -Eo "[0-9,]*"
232,184

-Eo "[0-9,]* views" = get the right number result is 232,184 views maybe this is enough.
-Eo "[0-9,]*" = extract the number result is 232,184.


All times are GMT -5. The time now is 01:11 PM.