Posts Tagged With: string

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.

http://www.oracle.com/technology/pub/articles/advanced-linux-commands/part2.html

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

[tags]awk,sed,shell,script,output,filter,text,cli,linux,string,separator[/tags]

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
No Comments »

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

[tags]perl,command,rename,replace,string[/tags]

No Comments »