我每次脚本输出之前打印出一份声明 [英] Print out a statement before each output of my script
问题描述
我有一个脚本来检查单词作者文件夹中的每个文件,然后打印出的次数,每个文件一行,为了从最高到最低。在总我有825文件。一个例子输出是
I have a script that checks each file in a folder for the word "Author" and then prints out the number of times, one line per file, in order from highest to lowest. In total I have 825 files. An example output would be
53
22
17
我想使它这样我在每条线上的数字之前打印出来的东西。这将是以下 hotel_ $ I
所以上面的例子现在是:
I want to make it so that I print out something before each number on every line. This will be the following hotel_$i
so the above example would now be:
hotel_1 53
hotel_2 22
hotel_3 17
我试图在我的shell脚本使用for循环这样做:
I have tried doing this using a for loop in my shell script:
for i in {1..825}
do
echo "hotel_$i"
find . -type f -exec bash -c 'grep -wo "Author" {} | wc -l' \; | sort -nr
done
但基本上打印出 hotel_1
,然后进行搜索和排序的所有825文件,那么 hotel_2
重复搜索和分类等。我如何让这个它的每一个输出前打印?
but this basically prints out hotel_1
, then does the search and sort for all 825 files, then hotel_2
repeats the search and sort and so on. How do I make it so that it prints before every output?
推荐答案
您可以使用粘贴
命令,它结合了不同的文件行:
You can use the paste
command, which combines lines from different files:
paste <(printf 'hotel_%d\n' {1..825}) \
<(find . -type f -exec bash -c 'grep -wo "Author" {} | wc -l' \; | sort -nr)
(只是把这个两行的可读性,可以是一个班轮没有 \\
)
这收割机粘贴
与进程替换,使得指挥模样的文件(命名管道)的输出粘贴
。
This combines paste
with process substitution, making the output of a command look like a file (a named pipe) to paste
.
第一个命令打印 hotel_1
, hotel_2
在单独的行等各,第二个命令是你的找到
命令。
The first command prints hotel_1
, hotel_2
etc. on a separate line each, and the second command is your find
command.
有关短期输入文件,输出看起来是这样的:
For short input files, the output looks like this:
hotel_1 7
hotel_2 6
hotel_3 4
hotel_4 3
hotel_5 3
hotel_6 2
hotel_7 1
hotel_8 0
hotel_9 0
这篇关于我每次脚本输出之前打印出一份声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!