Difference between revisions of "Linux"

From Wiki2
(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...")
 
Line 36: Line 36:
</code>
</code>


==regex==
{{: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===
*http://e-texteditor.com/blog/2010/beyond-vi
*http://www.grymoire.com/Unix/Regular.html#uh-2
*http://www.regular-expressions.info/php.html
===expressions===
If you want to get'Aloha World'out
 
Input:
 
<div class="test">Hello World</div>
<div class="somename">Aloha World</div>
<div>Hey There</div>
 
RegEx:
 
<pre>  \<div\sclass\=\"somename\"\>(?<Text>.*?)\<\/div\></pre>
 
Yields:
 
Aloha World (note: In a single group named Text)

Revision as of 11:30, 17 January 2013

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

https://alligator.io/js/regular-expressions-for-regular-people/

http://exploringjs.com/impatient-js/ch_regular-expressions.html

https://www.smashingmagazine.com/2019/02/regexp-features-regular-expressions/

https://regex101.com/

php example

 $search = array(
   '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
   '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
   '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
   '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
 );
   $output = preg_replace($search, , $input);
   return $output;
 }

?> <?php function sanitize($input) {

   if (is_array($input)) {
       foreach($input as $var=>$val) {
           $output[$var] = sanitize($val);
       }
   }
   else {
       if (get_magic_quotes_gpc()) {
           $input = stripslashes($input);
       }
       $input  = cleanInput($input);
       $output = mysql_real_escape_string($input);
   }
   return $output;

}

typical

A username fro 3- 16 characters w -or_ /^[a-z0-9_-]{3,16}$/

a password 6-18 long /^[a-z0-9_-]{6,18}$/

a hex number /^#?([a-f0-9]{6}|[a-f0-9]{3})$/

a number sign is optional because it is followed a question mark but if there takes it(greedy)
6 characters or 3 character

a slug /^[a-z0-9-]+$/

beginning of the string (^), followed by one or more (the plus sign) letters, numbers, or hyphens. and the end of the string ($).

an email /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

. get delimited \.
each section in parens

only lower case, nubers or dots'/[^a-z0-9.]/'

url /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

s? s if you got em
delimit / and . with \
you don't need http at all (https?:\/\/)? but be greedy
\d is any number
([\da-z\.-]+) numbers letters dotts or hyphens
\.([a-z\.]{2,6})~ .com .co .commie
([\/\w \.-]*)*\/?$/ files and directories
([\/\w \.-]*) * is zero or more, \w is words

html tag /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/

.* any characters
([^<]+) any characters but a <
(?:>(.*)<\/\1>|\s+\/>) non-capture group.it will contain either a greater than sign, some content, and a closing tag; or some spaces, a forward slash, and a greater than sign \1 represents the content that was captured in the first capturing group

no dots: ^[^\.]*$

^ - beginning of string
[^\.]* - any character except ., any number of repetitions
$ - end of string

replace spaces and punctuation with nothing str.replace(/\W/g, )

\W not letters digits or underscores

to take out all non alpha characters (Ruby)

 test_str = "Madam, I'm Adam"
 str = test_str.gsub(/[^a-zA-Z]/,'')
 puts str
returns MadamImAdam


  • ^[ \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)