12450

How do I write a Unix script which counts the number of reviews in a file?

Question:

I have a number of files which each contain reviews of a hotel and I would like to write a script which counts the number of reviews per file. An example of one file name would be hotel_73757. The text in each file is laid out as follows:

<Overall Rating>3.5 <Avg. Price>$260 <URL>http://www.tripadvisor.com/ShowUserReviews-g31310-d73757-r23009196-Wyndham_Phoenix-Phoenix_Arizona.html <Author>TexasSharvi <Content>the new updo is ... it's great! <Date>Dec 26, 2008 <No. Reader>-1 <No. Helpful>-1 <Overall>4 <Value>4 <Rooms>4 <Location>4 <Cleanliness>5 <Check in / front desk>5 <Service>-1 <Business service>4 <Author>ChrisLongo <Content>Just Dirty... Will never stay at any Wyndham hotel again. <Date>Dec 24, 2008 <No. Reader>1 <No. Helpful>1 <Overall>1 <Value>1 <Rooms>1 <Location>1 <Cleanliness>1 <Check in / front desk>1 <Service>1 <Business service>-1

This then repeats with a single line gap between each review, every review has the same fields. I was thinking of checking the number of times "Author" appears in each file would this work? Thanks in advance

Answer1:

Just use

grep -c "Author" yourFile

If you really want to make a script of this:

#!/bin/bash ################################################################################ # countreviews.sh # Counts the number of times the word "<Author>" appears in the specified file. ################################################################################ grep -c "^<Author>" "$1"

Make it executable with:

chmod +x countreviews.sh

And run it with:

./countreviews.sh file

or

./countreviews.sh "file with space in name"

Answer2:

you can use grep and wc to get the number of lines containing the word 'Author' appear in the file:

grep Author fileName | wc -l

grep will filter only the Author lines, wc -l will count them

Recommend

  • Erlang gen_server comunication
  • JT400 Read File From IFS with user without password
  • View local NPM packages that depend on package X
  • javascript sendfile binary data to web service
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • Reloading table causes flickering
  • Can you build a truly RESTful service that takes many parameters?
  • d3.js selection conditional rendering
  • How to create a new Bundle object?
  • How to Add Polymorphic Comments to Feed?
  • Why does the font in these TD elements render at different sizes?
  • Are there any issues with placing image as background on an extended JFrame?
  • Caliburn Micro, How to use ContentControl (or display 'sub' ViewModel) using ViewModel Fir
  • mave 3.2 not able to access local nexus instance return 502 code
  • HttpClient: disabling chunked encoding
  • HttpURLConnection Closing IO Streams
  • blade.php method outputting it's result to the form
  • Enabling DTD support in Sql Server
  • How to access culture data in globalize.js V1.0.0
  • xcode don't localize specific strings
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Selenium to click on a javascript button corresponding to a text
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Simulate click Geckofx vb,net
  • IE7 and TinyMCE with Plone
  • How to view images from protected folder with php?
  • Moving Android View and preventing onDraw to be called over and over again
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • How to have background script and something similar to a default popup?
  • How to attach a node.js readable stream to a Sendgrid email?
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • jquery mobile loadPage not working
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • JaxB to read class hierarchy