Linux

From Wiki2
Revision as of 11:29, 17 January 2013 by Tim (talk | contribs) (Created page with "==unix== ===perl=== *[http://download.boulder.ibm.com/ibmdl/pub/software/dw/library/l-p102/tomc.txt one liners] *http://www.somacon.com/p127.php ====handy code==== =====remo...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

unix

perl

handy code

remove blank lines from file

perl -wnl -e 'print $_ unless /^$/' infile.txt >outfile.txt

sed awk

lib/images/toolbar/list_ul.png
lib/images/toolbar/rule.png
lib/tpl/default/images/interwiki.png

People with shell access to their server can copy the text above and paste it into a file on the system, then run the following commands to check for and remove all those files. Only those that exist are removed.

grep -Ev "^($|#)" /tmp/removeold.txt | xargs -n 1 rm -f

If you are paranoid, replace the "rm -f" with "ls -la" to see what files will be deleted.

To remove directories as well as files you have to use: grep -Ev "^($|#)" /tmp/removeold.txt | xargs -n 1 rm -fd

However, some systems may not support the "rm -d" option for directory removal. In that case, you have to use recursive removal (just be sure to double-check that the file list does not include any paths that will delete too much): grep -Ev "^($|#)" /tmp/removeold.txt | xargs -n 1 rm -fr

regex

  • ^[ \t]+ //finds all the space and tabs
  • [1-9]\. //finds all the line numbers. (replace with #)
  • [A-E]\. //find A. etc (replace with ##)

to clean special characters from a string

$clean = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $text);

sites

expressions

If you want to get'Aloha World'out

Input:

Hello World
Aloha World
Hey There

RegEx:

  \<div\sclass\=\"somename\"\>(?<Text>.*?)\<\/div\>

Yields:

Aloha World (note: In a single group named Text)