Posts Tagged With: string

Replacing in Found Files

Tagged with: , , , , , ,

Replace one string by another in all file found by find.

find . -name *whatyouwant* -exec perl -pi.bak -e 's/TEXT_TO_REPLACE/TEXT_TO_REPLACE_IT_WITH/g' {} \;
No Comments »

Random Password in PHP

Tagged with: , , , , ,

A simple way to create a random password in PHP

$password = substr(md5(time()), 0, rand(4,8));

Seach for a String in a Folder using grep

Tagged with: , , , , ,

Search string “test_text” at directory ‘/var/log’ and below

grep test_text -R /var/log/*
No Comments »

Using M4 Command

Tagged with: , , , , ,

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.

No Comments »

Command to Print Until Regular Expression

Tagged with: , , , , ,

Print a file until a regular expression is matched.

cat file.txt | perl -pe "exit if(/Thats all/)"
No Comments »

Fetch columns of Output using Awk

Tagged with: , , , , , , , , , ,

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}'


No Comments »

Replace \n with another String

Tagged with: , , , ,

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
1 Comment »

Searching text in a directory using find and grep

Tagged with: , , , , , , ,

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\=]' {} \;
No Comments »

Command to Replace a String in all Files in Current Directory

Tagged with: , , , ,

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;" *


No Comments »