Seach for a String in a Folder using grep
Tagged with: command, folder, grep, Linux, search, string
Search string “test_text” at directory ‘/var/log’ and below
grep test_text -R /var/log/*
Search string “test_text” at directory ‘/var/log’ and below
grep test_text -R /var/log/*
This command takes an input file and substitutes strings inside it with the parameters passed, similar to substituting for variables. For example, here is an input file:
$ cat temp
The COLOR fox jumped over the TYPE fence.
Were you to substitute the strings “COLOR” by “brown” and “TYPE” by “broken”, you could use:
$ m4 -DCOLOR=brown -DTYPE=broken temp
The brown fox jumped over the broken fence.
Else, if you want to substitute “white” and “high” for the same:
$ m4 -DCOLOR=white -DTYPE=high temp
The white fox jumped over the high fence.
http://www.oracle.com/technology/pub/articles/advanced-linux-commands/part2.html
Print a file until a regular expression is matched.
cat file.txt | perl -pe "exit if(/Thats all/)"
One can use awk to get a specific column from a command output…
First column of the command
date|awk '{print $1}'
11th Column of the command
ps aux|awk '{print $11}'
Use a different Field separator
cat /etc/passwd|awk -F ':' '{print $1}'
[tags]awk,sed,shell,script,output,filter,text,cli,linux,string,separator[/tags]
The command to convert all \n in a file to another string - very useful for list code generation.
perl -ne 's/\\n/\',\'/g;print;' file.txt>new.txt
Use find and grep to search for a specific text in a directory. In this example, we search for files which uses the PHP short tag - <?
find -name '*.php' -exec grep '<?[^p\=]' {} \;
Replaces all instance of ‘<old string>’ with ‘<new sting>’ in all the files of the current directory.
perl -pi -e "s/<old string>/<new string>/g;" *
[tags]perl,command,rename,replace,string[/tags]