wc

words counting in file like linux wc command in C

我只是一个虾纸丫 提交于 2019-11-28 10:49:16
问题 I am trying to write something that works like the Linux command wc to count words, new lines and bytes in any kind of files and i can only use the C function read. I have written this code and i am getting the correct values for newlines and bytes but i am not getting the correct value for counted words. int bytes = 0; int words = 0; int newLine = 0; char buffer[1]; int file = open(myfile,O_RDONLY); if(file == -1){ printf("can not find :%s\n",myfile); } else{ char last = 'c'; while(read(file

shell 获取字符串的长度

▼魔方 西西 提交于 2019-11-28 08:52:55
awk 方式 bogon:conf macname$ echo "abcde" | awk '{print length($0)}' 5 利用${#str}来获取字符串的长度 bogon:conf macname$ a="abcdef" bogon:conf macname$ echo ${#a} 6 wc 方式(注意是包含换行符的) bogon:conf macname$ echo "abcde" | wc -c 6 参考: https://www.jb51.net/article/121290.htm 来源: https://www.cnblogs.com/sea-stream/p/11403206.html

NBUT 1749 论WC串的唯一性(思维)

拟墨画扇 提交于 2019-11-28 08:33:16
https://ac.2333.moe/Problem/view.xhtml?id=1749 wc 在爬塔时遇到了一串神秘字符,隐隐之中有一股力量从中透出 wc 很快发现了玄机,这个字符串中每一个含有“wc”的连续子序列都能为wc提供魔法值 找出字符串能为wc提供多少魔法值 注意如果某个连续子序列中有2个或以上“wc”,会导致魔法过剩,其魔法值为0 Input 第一行,数据组数T(1<=T<=10) 接下来T行,每行一个字符串s,有小写英文字母组成 1<=|S|<=10^5 Output 输出T行 对于每组数据,输出只含有一个“wc”作为连续子序列的个数 Sample Input 3 wcak woc awcawc Sample Output 3 0 9 Hint For first sample : wc,wca,wcak For third sample: awc,awca,awcaw,wc,wca,wcaw,cawc,awc,wc; #include<iostream> #include<algorithm> using namespace std; int main() { int T; cin >> T; while(T--) { string s; cin >> s; int last = 0; int id = s.find("wc"); long long left

How to count lines of code including sub-directories [duplicate]

冷暖自知 提交于 2019-11-28 02:46:07
This question already has an answer here: How to count all the lines of code in a directory recursively? 42 answers Suppose I want to count the lines of code in a project. If all of the files are in the same directory I can execute: cat * | wc -l However, if there are sub-directories, this doesn't work. For this to work cat would have to have a recursive mode. I suspect this might be a job for xargs, but I wonder if there is a more elegant solution? philant First you do not need to use cat to count lines. This is an antipattern called Useless Use of Cat (UUoC). To count lines in files in the

get just the integer from wc in bash

為{幸葍}努か 提交于 2019-11-27 17:25:22
Is there a way to get the integer that wc returns in bash? Basically I want to write the line numbers and word counts to the screen after the file name. output: filename linecount wordcount Here is what I have so far: files=`ls` for f in $files; do if [ ! -d $f ] #only print out information about files !directories then # some way of getting the wc integers into shell variables and then printing them echo "$f $lines $ words" fi done You can use the cut command to get just the first word of wc 's output (which is the line or word count): lines=`wc -l $f | cut -f1 -d' '` words=`wc -w $f | cut

wc cut uniq sort 命令

坚强是说给别人听的谎言 提交于 2019-11-27 05:54:06
学习 wc cut uniq sort 四个命令 : wc :wc用来显示标准输出或者输入的文件的行、单词、字节个数。 wc-l: wc –l /etc/inittab(显示行数) wc-c: wc –c /etc/inittab(显示字节数) wc-m: wc –m /etc/inittab(显示字符数) wc-w: wc –w /etc/inittab(显示字节数) wc-L: wc –L /etc/inittab(显示最长一行字节数) cut-b : 按照字节来切割这个语句(空格也算一个字符) -b不能识别中文,在英文环境下-b和-c一样 例如:cat test.txt I am oldboy my qq is 1234567 1、cut –b 3-4 test.txt (切割test.txt文件中第三和第四个字节:am) 2、cut –b -4 test.txt(切割test.txt文件中第四个字节之前的所有字节,包括第四个:I am) 3、cut –b 1,4- test.txt(切割test.txt文件中第一个字节和第四个字节之后的所有字节:Im oldboy my qq is 1234567) cut –c : 按照字符来切割 除了能切割中文,英文与-b相同 cut –d: 指定分隔符(默认以tab键作为分隔符) 例如:head -1 /etc/passwd

文本处理器 - wc cut sort uniq

爷,独闯天下 提交于 2019-11-27 04:47:38
wc   word count统计文本文件中的字符个数   用法: wc filename   行数 字符个数 文件大小 (字节) 文件名   -l   -w   -c cut   用来做文件分隔   -d 指定分隔符(delimiter)     -d[ :]不行   -f 指定输出的列数据:     -f2     -f1-3     -f1,3   --output-delimiter='xx'   [cut的局限性:1、-d指定分隔符,不能同时指定多个2、不能做高级的格式化输出;所以我要熟练掌握awk] sort   排序,默认查看第一个字符(包括数字字母以及空格和特殊字符),以ASCII码来排序(大小写不是)   -f 忽略大小写的差异, 例如A与a视为编码相同;   -b忽略最前面的空格符部分;   -M以月份的名字来排序,例如JAN DEC等等的排序方法;   -n使用纯数字进行排序,默认是以文件形态来排序的;   -r反向排序;   -u就是uniq,相同的数据中,仅出现一行代表;   -t分隔符,默认是用tab键分割;   -k以那个区间(field)来进行排序的意思 uniq   连续且相同的命令,才被视为重复   建议,先排序,后去重 tar 归档-》 对目录   -c统计某些字符重复次数(重要)   sort filename | uniq -c 来源:

How to get “wc -l” to print just the number of lines without file name?

情到浓时终转凉″ 提交于 2019-11-27 02:59:06
wc -l file.txt outputs number of lines and file name. I need just the number itself (not the file name). I can do this wc -l file.txt | awk '{print $1}' But maybe there is a better way? Norman Ramsey Try this way: wc -l < file.txt cat file.txt | wc -l According to the man page (for the BSD version, I don't have a GNU version to check): If no files are specified, the standard input is used and no file name is displayed. The prompt will accept input until receiving EOF, or [^D] in most environments. To do this without the leading space, why not: wc -l < file.txt | bc Neil Albert How about wc -l

How to count lines fast?

一笑奈何 提交于 2019-11-27 02:31:36
问题 I tried unxutils' wc -l but it crashed for 1GB files. I tried this C# code long count = 0; using (StreamReader r = new StreamReader(f)) { string line; while ((line = r.ReadLine()) != null) { count++; } } return count; It reads a 500MB file in 4 seconds var size = 256; var bytes = new byte[size]; var count = 0; byte query = Convert.ToByte('\n'); using (var stream = File.OpenRead(file)) { int many; do { many = stream.Read(bytes, 0, size); count += bytes.Where(a => a == query).Count(); } while

How to count lines of code including sub-directories [duplicate]

a 夏天 提交于 2019-11-26 23:48:28
问题 This question already has an answer here: How to count all the lines of code in a directory recursively? 43 answers Suppose I want to count the lines of code in a project. If all of the files are in the same directory I can execute: cat * | wc -l However, if there are sub-directories, this doesn't work. For this to work cat would have to have a recursive mode. I suspect this might be a job for xargs, but I wonder if there is a more elegant solution? 回答1: First you do not need to use cat to