Wednesday, November 12, 2014

Simple Javascript

JavaScript - Example form
Selection:
Enter text:

Monday, November 10, 2014

Using Bash and Sed for Quick String Manipulations

To print the first string starting with an _  :

$ tmp="ffuts erom dna erom _this and _that _is here ___okay"

## Mark the first string for extraction, then use sed to grab it and remove the marker
$ echo "${tmp2/ _/ XXX_}" | sed 's/.*\(.*XXX[^ ]*\).*/\1/;s/XXX//'

Saturday, November 8, 2014

Dynamically building AWK statements w/eval

E.g. This will sum the column entries entered on the command line

#!/bin/bash
eval "awk '{print $(echo "${*:2}" | sed -r 's/\b[0-9]*\b/\$&+/g;s/\+$//')}' $1" 
E.g. cat num.txt
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1

$>my.awk num.txt 2 3
7
7
7
7
7
7
7
7
7
7 

Thursday, November 6, 2014

Sed to modify every 1st and 3rd occurrence

sed 'H;$!d;x;:a;s/word/MATCH/1;s/word/IGNORE/1;s/word/MATCH/1;t a;s/MATCH/words/g;s/IGNORE/word/g' file

Okay, this may seem complicated at first but its simple - this copies all the text into the Hold space, after that it uses a 3 pattern first occurence match to modify the looked for text (word) with a substitute constant (to avoid reprocessing) then loops (t a) back to the label (:a) until the whole file is processed afterward it replaces the substitute constants.

You can use whatever substitute constants (MATCH,IGNORE) you want if those are problem for your data.

Markup tag stripping w/ SED

<?xml version="1.0" encoding="UTF-8" ?>
<Attributes>
   <Attribute>123</Attribute>
   <Attribute>959595</Attribute>
   <Attribute>1233</Attribute>
   <Attribute>jiji</Attribute>
</Attributes> 
To pull the second attribute:  
sed -n '/<Attributes>/,\#</Attributes>#{/<Attribute>/{
 n;s#.*<Attribute>\(.*\)</Attribute>.*#\1#;p;q};}'
 

Tuesday, November 4, 2014

Process Multivariables w/ read and a while Loop

To process data that requires you to pull multiple segments into variables, instead of a nested for loop or additional statements that would be clumsy and "awkward" (in a non-awk way) you can use a while loop and break the data into fields using awk or cut or your favorite method and read the data straight into variables:      

while read a b; do ((! $(grep -c "$a.*$b" File2.txt))) && (echo "$a $b" >> missing_pkgs.txt); done < <(awk '/>=/{ print $1" "$3 }' File1.txt)