本文的目的是介绍BASH–计算文件中类似行的数量的详细情况,特别关注shell计算文件行数的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解BASH–计算文件中类似行的
本文的目的是介绍BASH – 计算文件中类似行的数量的详细情况,特别关注shell计算文件行数的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解BASH – 计算文件中类似行的数量的机会,同时也不会遗漏关于awk – 计算文本文件中的字符串数、bash – docker ps -a |的Shell脚本grep查找运行的某些容器的数量、bash – Unix:计算第一列中类似条目的出现次数,对第二列求和、bash – 使用命令行工具计算文件中的行长度的知识。
本文目录一览:- BASH – 计算文件中类似行的数量(shell计算文件行数)
- awk – 计算文本文件中的字符串数
- bash – docker ps -a |的Shell脚本grep查找运行的某些容器的数量
- bash – Unix:计算第一列中类似条目的出现次数,对第二列求和
- bash – 使用命令行工具计算文件中的行长度
BASH – 计算文件中类似行的数量(shell计算文件行数)
文件结构示例:
Join Date: Apr 2005 Location: bama via new orleans Age: 48 Posts: 2,369 Re: Top 10 Songs Jethro Tull oh dearrrr. the only way for all kaths to keep their last shred of sanity: fly through this list as quickly as possible,without stopping to think for a microsecond... velvet green dun ringill skating away on the thin ice of a new day sossity yer a woman fat man life's a long song jack-a-lynn teacher mother goose elegy 03-10-2010,02:29 AM #5 (permalink) Sox Avoiding The Swan Song Join Date: Jan 2010 Location: Derbyshire,England Age: 43 Posts: 5,991 Re: Top 10 Songs Jethro Tull Wow !!!! Where do I start ? Dun Ringill Aqualung With You There To Help Me Jack Frost And The Hooded Crow We Used To KNow Witch's Promise Pussy Willow Heavy Horses My Sunday Feeling Locomotive Breath Join Date: Nov 2009 Posts: 1,418 Re: Top 10 Songs Jethro Tull Too bad they all can't make the list,but here's ten I never get tired of listening to: Christmas Song Witches Promise Life's A Long Song Living In The Past Rainbow Blues Sweet Dream Minstral In The gallery Cup of Wonder Rover Something's On the Move
输出示例:
life's a long song 3 aqualung 1 ...
假设您在名为input的文件中拥有所有内容,请尝试:
tr '[A-Z]' '[a-z]' < input | \ egrep -v "^ *(join date|age|posts|location|re):" | \ sort | \ uniq -c
第一行缩小所有内容,第二行删除样本中看起来像电子邮件标题的内容,然后对唯一项进行排序和计数.
awk – 计算文本文件中的字符串数
aab abb 263-455 aab abb 263-455 aab abb 263-455 bbb abb 26-455 bbb abb 26-455 bbb aka 264-266 bga bga 230-232 bga bga 230-232
我想根据第三列的数量计算第一列和第二列中每个字符串的唯一编号.
输出:
aab - 1 abb - 2 bbb - 2 aka - 1 bga - 2 Total no - 8
解决方法
awk ' !s[1":"$1":"$3]++{sU[$1]++;tot++} !s[2":"$2":"$3]++{sU[$2]++;tot++} END{ for (x in sU) print x,sU[x]; print "Total No -",tot; }' input
产量
bga 1 aab 1 bbb 2 aka 1 bga 1 abb 2 Total No - 8
bash – docker ps -a |的Shell脚本grep查找运行的某些容器的数量
下面是我需要的bash脚本的伪代码
var name = $1 var number_of_results = # of containers returned from $(docker ps -a | grep "$name") if(number_of_result > 0) docker rm -f $(docker ps -a | grep "$name")
#!/bin/bash name=$1 matchingStarted=$(docker ps --filter="name=$name" -q | xargs) [[ -n $matchingStarted ]] && docker stop $matchingStarted matching=$(docker ps -a --filter="name=$name" -q | xargs) [[ -n $matching ]] && docker rm $matching
基本上,它会检查是否有一个带有提供名称的正在运行的容器,并在找到它时停止它.然后,它删除任何具有提供名称的容器.
注意:您可能希望添加一些参数验证,就像使用不带参数一样,此脚本将停止所有正在运行的容器并删除所有已停止的容器.我没有在这里添加它以保持简单易读.
bash – Unix:计算第一列中类似条目的出现次数,对第二列求和
示例列表:
2013-11-13-03 1 2013-11-13-06 1 2013-11-13-13 2 2013-11-13-13 1 2013-11-13-15 1 2013-11-13-15 1 2013-11-13-15 1 2013-11-13-17 1 2013-11-13-23 1 2013-11-14-01 1 2013-11-14-04 6 2013-11-14-07 1 2013-11-14-08 1 2013-11-14-09 1 2013-11-14-09 1
我希望输出读取类似于以下内容
2013-11-13-03 1 1 2013-11-13-06 1 1 2013-11-13-13 2 3 2013-11-13-15 3 3 2013-11-13-17 1 1 2013-11-13-23 1 1 2013-11-14-01 1 1 2013-11-14-04 1 6 2013-11-14-07 1 1 2013-11-14-08 1 1 2013-11-14-09 2 2
第1列是前面示例第1列中的匹配列,第2列是前一示例中第1列的匹配计数(如果没有其他匹配,则为1),第3列是来自匹配列1的第2列的总和前面的例子.任何人都有使用awk或uniq和awk的混合物完成此任何提示?
解决方法
awk ' { counts[$1]++; # Increment count of lines. totals[$1] += $2; # Accumulate sum of second column. } END { # Iterate over all first-column values. for (x in counts) { print x,counts[x],totals[x]; } } ' file.txt | sort
如果您不关心输出行的顺序,可以跳过排序.
bash – 使用命令行工具计算文件中的行长度
如果我有一个长文件包含许多不同长度的行,我如何计算每行长度的出现次数?
例:
file.txt
this is a sample file with several lines of varying length
运行count_line_lengths file.txt将给出:
Length Occurences 1 1 2 2 4 3 5 1 6 2 7 2
想法?
{ print length($0); }
… …
$ awk -f count.awk input.txt | sort | uniq -c 1 1 2 2 3 4 1 5 2 6 2 7
关于BASH – 计算文件中类似行的数量和shell计算文件行数的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于awk – 计算文本文件中的字符串数、bash – docker ps -a |的Shell脚本grep查找运行的某些容器的数量、bash – Unix:计算第一列中类似条目的出现次数,对第二列求和、bash – 使用命令行工具计算文件中的行长度的相关信息,请在本站寻找。
本文标签: