如何列出文字超过所有子目录中的n个字符的文件 [英] How to list files with words exceeding n characters in all subdirectories

查看:163
本文介绍了如何列出文字超过所有子目录中的n个字符的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写创建一个包含每个文本文件的文件名从文件夹中的文件中的shell脚本(给定的参数),它是包含单词超过n个字符的子文件夹(读取n从键盘)。

I have to write a shell script that creates a file containing the name of each text files from a folder (given as parameter) and it's subfolders that contain words longer than n characters (read n from keyboard).

我写了下面code迄今:

I wrote the following code so far :

#!/bin/bash

验证,如果第一个给定的参数是一个文件夹:

if [ ! -d $1 ]
then echo $1 is not a directory\!
exit 1
fi

读取n个

echo -n "Give the number n: "
read n
echo "You entered: $n"

目标在哪里写的文件的名称:

destinatie="destinatie"

,我认为它使我的问题的实际部分:

nr=0;
#while read line; 
#do
for fisier in `find $1 -type f`
    do  
        counter=0
        for word in $(<$fisier);
            do
                file=`basename "$fisier"`
                length=`expr length $word`
                echo "$length"
                if [ $length -gt $n ];
                    then counter=$(($counter+1))
                fi
            done 
    if [ $counter -gt $nr ];
    then echo "$file" >> $destinatie
    fi
    done
break           
done
exit 

该脚本工作,但它确实多了一些步骤,我不need.It好像它读取一些文件超过1次。如果有人能帮助我吗?

The script works but it does a few more steps that i don't need.It seems like it reads some files more than 1 time. If anyone can help me please?

推荐答案

这是否帮助?

egrep -lr "\w{$n,}" $1/* >$destinatie

一些解释:

\\ W 表示:该字包含的字符

\w means: a character that words consist of

{$ N,} 表示:的连续字符数至少 $ N

{$n,} means: number of consecutive characters is at least $n

选项 -l <​​/ code>列出文件,不打印grepped文字和<$​​ C $ C> -r 上执行递归扫描你的在 $ 1

Option -l lists files and does not print the grepped text and -r performs a recursive scan on your directory in $1

编辑:

周围egrep命令多一点完整版:

a bit more complete version around the egrep command:

#!/bin/bash

die() { echo "$@" 1>&2 ; exit 1; }

[ -z "$1" ] && die "which directory to scan?"

dir="$1"
[ -d "$dir" ] || die "$dir isn't a directory"

echo -n "Give the number n: "
read n
echo "You entered: $n"
[ $n -le 0 ] && die "the number should be > 0"

destinatie="destinatie"
egrep -lr "\w{$n,}" "$dir"/* | while read f; do basename "$f"; done >$destinatie

这篇关于如何列出文字超过所有子目录中的n个字符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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