linux - 打印有关文本文件的统​​计信息的 Bash 脚本

标签 linux bash shell unix scripting

我正在尝试编写一个 Linux bash 脚本,它将帮助我从文本文件生成一些我需要的统计信息。假设我正在使用的文本文件采用以下格式:

"string : pathname1 : pathname2 : pathname3 : … pathnameN"

其中路径名“i ”是我在其中找到特定字符串的文件的完整路径。例如,这样的文件可能如下所示:

logile.txt

string : "version" pathname1: /home/Desktop/myfile.txt pathname2 : /usr/lib/tmp/sample.txt 

string : "user" pathname1 : temp1/tmpfiles/user.txt  pathname2 : newfile.txt pathname3 : /Downloads/myfiles/old/credentials.txt

string : "admin" pathname1 : 

string: "build" pathname1 : Documents/projects/myproject/readme.txt pathname2 
 : Desktop/readmetoo.txt

在此示例中,我希望 bash 脚本通知我,我总共搜索了 4 个单词(版本、用户、管理员、构建),并且在大多数文件中找到的单词是“user”,它在 3 个文件中找到。使用“awk”命令是一个好方法吗?我不熟悉 bash 脚本,所以任何帮助都会很有用!谢谢!

最佳答案

这不是一个容易的任务,它可能可以单独用 awk 完成,但你问的是 bash 脚本。以下 bash 脚本:

#!/bin/bash
set -euo pipefail

wordcount=0
tmp=""
# for each line in input file read 3 fields
while read string name rest; do
    # in each line the first word should be equal to string
    if [ "$string" != string ]; then 
        # if it isn't continue to the next line
        continue; 
    fi
    # remove '"' from name
    name=${name//\"}
    # the rest has a format of pathname <path> 
    # substitute every space with newline in the rest 
    # and remove lines containing pathname'i' 
    # to get only paths in the rest
    rest=$(echo "$rest" | tr ' ' '\n' | grep -v "pathname" ||:)
    # count the lines in rest, each path should be in different line
    restcnt=$(wc -l <<<"$rest")
    # save restcnt and name in single line in tmp string to parse later
    tmp+="$restcnt $name"$'\n'
    # increment wordcount
    wordcount=$[$wordcount+1]

# feed while read loop with a command substitution
done < <(
    # cat stdin or file given as script argument
    # and substitude all doublepoints ':' with spaces
    cat "$@" | sed 's/:/ /g'
)

# sort the tmp string from the lowest to biggest
# and get last line (ie. biggest number
tmp=$(echo "$tmp" | sort -n | tail -n1)
# split tmp into two variables - the word and the count
read mostcnt mostfile <<<"$tmp"

# and print user information
echo "You searched for a total of $wordcount words."
echo "The word that was found in most files was: $mostfile"
echo " which was found in $mostcnt files."

...使用以下logile.txt中的输入运行...

string : "version" pathname1:/home/Desktop/myfile.txt pathname2 : /usr/lib/tmp/sample.txt

string : "user" pathname1: temp1/tmpfiles/user.txt pathname2: newfile.txt pathname3: /Downloads/myfiles/old/credentials.txt

string : "admin" pathname1 :

string: "build" pathname1: Documents/projects/myproject/readme.txt pathname2:Desktop/readmetoo.txt

...产生以下结果:

$ /tmp/1.sh <./logile.txt 
You searched for a total of 4 words.
The word that was found in most files was: user
 which was found in 6 files.

关于linux - 打印有关文本文件的统​​计信息的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50040803/

相关文章:

linux - 即使以 root 身份也无法执行 bash 脚本?

shell - 在debian系统下使用华为E3131和HiLink通过命令行发送和接收短信

linux - Sed 无法将临时文件复制到原始文件

shell - 在shell脚本的for循环中使用sqlite3会产生错误

c - 为什么gcc对函数中的局部变量重新排序?

linux - 如何在启动和关闭时启动和停止 bash 脚本?

linux - 触摸命令错误

python - Python Lambda打包和部署Bash

linux - 使用 AWK 并将结果设置为 bash 变量/数组?

java - 在Linux headless 机器中修改文件时触发java jar文件