How can I get the name of a file in a directory on unix based on grep sed or awk in unix or linux

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How can I get the name of a file in a directory on unix based on sed, grep, or awk in unix or linux?

I though I could do something like:

    for i in $(ls /tmp/files/date*); do
       if [ $(cat $i | head -n 1 | grep -c "6") >= 1 ] ; then
          echo $i
       fi
    done

I need it to search all of the files in a certain directory (only the first line in them each) and then return which files have the string in them.

Thanks,

 Tim

Answers

You can simplify your script a little bit and get the desired result:

for i in /tmp/files/date*; do
   if head -n 1 "$i" | grep -q "6"; then
      basename "$i"
   fi
done

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/10093505/how-can-i-get-the-name-of-a-file-in-a-directory-on-unix-based-on-grep-sed-or-a

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils