linux-统计一个文件中出现的单词数

#!/bin/bash
if [ $# -ne 1 ]
then
echo "Usage: $0 filename";
exit -1
fi

filename=$1
egrep -o "\b[[:alpha:]]+\b" $filename | awk ‘{ count[$0]++ }
END{ printf("%-14s%s\n", "word", "count");
for(ind in count)
{ printf("%-14s%d\n", ind, count[ind]); }
}‘
egrep -o "\b[[:alpha:]]+\b" $filename 可以得到文件中所有的单词 \b为单词边界标记符
时间: 2024-08-18 11:44:56

linux-统计一个文件中出现的单词数的相关文章

Java学习(4):统计一个文件中的英文,中文,数字,其他字符以及字符总数

要求:统计一个文件中的英文,中文,数字,其他字符以及字符总数(此随笔以txt文件为例) import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; /** * 将一个文件中英文,中文,数字,其

统计一个文件中出现字符'a'的次数

# -*- coding: utf-8 -*- #python 27 #xiaodeng #统计一个文件中出现字符'a'的次数 #http://www.cnblogs.com/hongten/p/hongten_python_count.html import os number=0 def getNumber(filePath,c): 'c---->the word numbers' #统计一个文件中出现字符'a'的次数 if os.path.exists(filePath): global

linux在所有文件中查找某一个字符

# find <directory> -type f -name "*.c" | xargs grep "<strings>" <directory>是你要找的文件夹:如果是当前文件夹可以省略-type f 说明,只找文件-name "*.c" 表示只找C语言写的代码,从而避免去查binary:也可以不写,表示找所有文件<strings>是你要找的某个字符串 sudo find -type f -n

linux命令---查找文件中的内容

linux命令---查找文件中的内容 [[email protected] ~]$ cat 1.txt |egrep '123456789|second'-------匹配123456789或者second的行 first line:123456789012345678901234567890123456789012345678901234567890 second line:one two three four five six seven eight nine ten [[email pro

剔除在另一个文件中出现的行

有两个文件A,B.我想要一个文件C,C中含有的是B文件中剔除与A相同行的数据. 用C++编写,用到fstream,ifstream,set以及vector. #include <fstream> #include <iostream> #include <vector> #include <set> using namespace std; struct strless : public std::binary_function<const char*

统计一个字符串中的单词的个数,并打印各个单词

/*测试数据:Shen zhen is a beautiful city!*/ /*运行结果:Word:6 Shen zhen is a beautiful city!*/ #include<stdio.h> #define SIZE 1000 void wordCount(char *str) { int count = 0, flag = 0; char *p = str; while (*p != '\0'){ while (*p == 32){ if (*(p + 1) == 0){/

Java IO把一个文件中的内容以字符串的形式读出来

代码记录(备查): /** * 把一个文件中的内容以字符串的形式读出来 * * @author zhipengs * */ public class FileToString { public static void main(String[] args) { System.out.println(readFileToString()); } private static String readFileToString() { // new 一个空文件,用于获取路径 File dirs = ne

Linux查看一个文件夹大小

1.Linux查看一个文件夹大小: du -sh /home/yangkun [[email protected] bin]$ du -sh /home/yangkun/ 164M /home/yangkun/ 2.Linux查看某个目录下所有文件的大小:du -h /home/yangkun [[email protected] bin]$ du -h /home/yangkun/ 4.0K /home/yangkun/.config/abrt 8.0K /home/yangkun/.conf

字符串之“统计一个字符串中单词的个数”

题目:统计一个字符串中单词的个数 输入一行字符,统计其中有多少个单词,单词之间用空格分隔开 输入:my name is jacky 输出:the number of word is 4 代码如下: #include <stdio.h> int main(int argc, char *argv[]) { char str[80]; int i=0,num=0,flag=0; char c; gets(str); while((c=str[i])!='\0') { if(c==' ') flag