Grep expression with special file names

Posted by user2919185 on Stack Overflow See other posts from Stack Overflow or by user2919185
Published on 2013-10-26T09:51:01Z Indexed on 2013/10/26 9:53 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

i am a real beginner in csh/tcsh scripting and that's why i need your help. The problem is I have to go through some regular files in directories and find those files, that have their own name in its content. In the following piece of script is cycle in which I am going through paths and using grep to find the file's name in its content. What is surely correct is $something:q - is array of paths where i have to find files. The next variable is name in which is only name of current file. for example: /home/computer/text.txt (paths) and: text.txt (name) And my biggest problem is to find names of files in their content. It's quite difficult for me to write correct grep for this, cause the names of files and directories that i am passing through are mad. Here are some of them:

/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/!_1
/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/dam/$user/:e/'/-r
/home/OS/pocitacove/testovaci_adresar/z/test3/skusime/ taketo/   taketo
/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/.-bla/.-bla/.a=b
/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/.-bla/.-bla/@
/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/.-bla/.-bla/:
/home/OS/pocitacove/testovaci_adresar/z/test4.pre_expertov/.-bla/.-bla/'ano'

foreach paths ($something:q)
    set name = "$paths:t"
    @ number = (`grep -Ec "$name" "$paths"`)

    if ($number != 0) then
        echo -n "$paths "
        echo $number
    endif

    @ number = 0
end

© Stack Overflow or respective owner

Related posts about unix

Related posts about script