我怎样写一个Unix脚本统计一个文件评语数量? [英] How do I write a Unix script which counts the number of reviews in a file?

查看:169
本文介绍了我怎样写一个Unix脚本统计一个文件评语数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多均含有一酒店的评价文件的,我想编写一个脚本,可计算每档评语数量。一个文件名的一个例子是 hotel_73757 。在每个文件的文本布局如下:

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

推荐答案

只需使用

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"

请其可执行文件:

chmod +x countreviews.sh

和与运行:

./countreviews.sh file

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

这篇关于我怎样写一个Unix脚本统计一个文件评语数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆