gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:59:51 25 4
gpt4 key购买 nike

我正在尝试编写一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com