LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2013, 05:26 AM   #1
tpopono
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
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?
 
Old 02-17-2013, 06:46 AM   #2
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
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.
 
Old 02-17-2013, 08:04 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Try:
Code:
~$ awk '/data-context-item-views/ {print $8}' inputfile.html | awk -F"=" '{gsub(/\"/,"",$0); print $2}'
 
1 members found this post helpful.
Old 02-17-2013, 11:53 AM   #4
tpopono
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-17-2013, 12:48 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by tpopono View Post
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.

Last edited by shivaa; 02-17-2013 at 12:49 PM.
 
Old 02-17-2013, 01:25 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
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.

Last edited by whizje; 02-17-2013 at 01:27 PM.
 
Old 02-17-2013, 01:40 PM   #7
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
[SOLVED] grep question aihaike Programming 3 07-28-2009 01:43 PM
grep question sudz28 Linux - Newbie 4 01-21-2009 08:13 PM
grep question tekmann33 Linux - Newbie 1 08-11-2008 12:22 PM
grep question keyops Programming 2 04-09-2004 10:17 PM


All times are GMT -5. The time now is 06:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration