bash脚本读取命令行的文件与文件列表 [英] Bash script to read a file from the command line with list of files

查看:635
本文介绍了bash脚本读取命令行的文件与文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个bash脚本,将采取从包含内部文件列表在命令行中输入(文件)。那么我就需要打开这些文件的文件中,并用字读字保持每个单词的出现计数在列表中的所有文件。到目前为止,它正在打印出来的文件内的文件的列表,而且文件本身。这是我到目前为止所。我是新的,所以我不知道如何做到这的bash脚本。我AP preciate任何帮助。谢谢

 #!/斌/庆典    wordArray =()
    countArray =()
    INPUT =$ 1;    如果[-f$ INPUT]
    然后
       找到$名字型的F
       回声$名称;
    其他
       回声$ INPUT不是一个文件!
    科幻


解决方案

要计算在列表中的所有文件中的所有单词的OCCURENCES在一个文件中,你可以使用:

  xargs的grep的-hoP'\\ B \\ W + \\ B'< file_with_list |排序| uniq的-c

例如:

文件LIST.TXT

  test1.txt的
的test2.txt

test1.txt的

 的Hello World

的test2.txt

 你好你好一词再次

运行:

  xargs的grep的-hoP'\\ B \\ W + \\ B'< LIST.TXT |排序| uniq的-c

打印

  1再次
   3个招呼
   2字

注意事项:


  • 在文件名中的 LIST.TXT 不能包含空格...

I need to write a bash script that will take in input (a file) from the command line which contains a list of files inside. I then need to open those files inside the file and read it word by word keeping a count of the occurrences of each word in all the files in the list. So far it is printing out the list of files inside the file, but also the file itself. This is what I have so far. I am new to bash scripts so I am not sure how to do this. I appreciate any help. Thanks

    #!/bin/bash

    wordArray=()
    countArray=()
    INPUT="$1";

    if [ -f "$INPUT" ]
    then
       find $name -type f
       echo "$name";
    else
       echo "$INPUT is not a file!";
    fi 

解决方案

To count the occurences of all words in all files from the list in one file, you can use:

xargs grep -hoP '\b\w+\b' < file_with_list | sort | uniq -c

Example:

file list.txt

test1.txt
test2.txt

test1.txt

hello world

test2.txt

hello word hello again

running:

xargs grep -hoP '\b\w+\b' < list.txt | sort | uniq -c

prints

   1 again
   3 hello
   2 word

Caveats:

  • the filenames in the list.txt can't contain spaces...

这篇关于bash脚本读取命令行的文件与文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆