Friday, August 15, 2014

One liner to parse parenthesized statements

msgbox: 
//messagebox("ñ",string(asc("ñ")))
//messagebox("Ñ",string(asc("Ñ")))
//messagebox("ñ",string(char(241)))

messagebox("Hi")

IF Trim(sle_user_id.text) = "" AND Trim(sle_password.text) = "" THEN
    MessageBox(Titulo_Msg,&
              "Sr Usuario :~r~nDebe ingresar los datos solicitados.",StopSign!,Ok!)
    sle_user_id.SetFocus()
    Return
End If

IFS=$(echo -en "\n\b") && for a in $(grep '[Mm]essage[Bb]ox' msgbox); do echo $a$((($(echo "$a" | grep -o '[()]' | wc -w)%2)) && sed -n "/$a/{n;p;}" msgbox) | perl -lne '{@mystr=$_=~m/\((?>[^()]|(?R))*\)/g; print "@mystr\n" if $_ =~ m/\((?>[^()]|(?R))*\)/g; }' | sed 's/^(\(.*\))$/\1/g';done && unset IFS

Output is:
"ñ",string(asc("ñ"))
"Ñ",string(asc("Ñ"))
"ñ",string(char(241))
"Hi"
Titulo_Msg,& "Sr Usuario :~r~nDebe ingresar los datos solicitados.",StopSign!,Ok!

sed -n '/x(/,/)$/{s/.*x(//;s/)$//p}' msgbox_file.txt

Output: 
"ñ",string(asc("ñ"))
"Ñ",string(asc("Ñ"))
"ñ",string(char(241))

"Hi"
Titulo_Msg,&
   "Sr Usuario :~r~nDebe ingresar los datos solicitados.",StopSign!,Ok! 

Tuesday, August 12, 2014

Diff 2 files for missing lines out of sequence

cat <(grep -x -v -F -f file2 -- <((grep -x -v -F -f file1 -- file2) >> file1;cat file1)) >> file2 

## reports any lines contained in <file 1> missing in <file 2>  
IFS=$(echo -en "\n\b") && for a in $(cat <file 1>); do ((\!$(grep -F -c -- "$a" <file 2>))) && echo $a; done && unset IFS

#!/usr/bin/env bash
if [[ $# -eq 2 ]]; then
## reports any lines contained in <file 1> missing in <file 2>  
IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do ((\!$(grep -F -c -- "$a" "$2"))) && echo $a; done && unset IFS
elif [[ $# -eq 3 ]]; then
IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$2"))) && echo $a; done && unset IFS
elif [[ $# -eq 4 ]]; then
 TMPFILE1="/tmp/_lgd_$$_f1.txt"
 TMPFILE2="/tmp/_lgd_$$_f2.txt"
 (IFS=$(echo -en "\n\b") && for a in $(cat "$1"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$2"))) && echo $a; done && unset IFS) >> "$TMPFILE1"
 cat "$TMPFILE1" >> "$2"
 (IFS=$(echo -en "\n\b") && for a in $(cat "$2"); do b=$(echo "$a" | sed "s/\(.\{$3\}\)\(.*\)/\1/");((\!$(grep -F -c -- "$b" "$1"))) && echo $a; done && unset IFS) >> "$TMPFILE2"
 cat "$TMPFILE2" >> "$1"
 rm "$TMPFILE1" "$TMPFILE2"
else
cat <<EOF
  Lgd: line grep diff, finds all the strings in file1 missing from file2 and optionally
       limits the grep search to the first # of chars
  Usage: lgd.bsh <file1> <file2> <chars> <flag sync both files>
   E.g. lgd.bsh  myaliases.bsh /tmp/old_myaliases.bsh 11 syncit
          
          lgd.bsh  oldlegalclauses.txt newlegalclauses.txt 500 syncit 
EOF
fi 

Monday, August 11, 2014

Passing command line args to bash alias

alias digq='_(){ dig +short txt "$1".wp.dg.cx; }; _'

this reports a quick description of whatever is queried

$ digq cray
"Cray Inc. is an American supercomputer manufacturer based in Seattle, Washington. The company's predecessor, Cray Research, Inc. (CRI), was founded in 1972 by computer designer Seymour Cray. Seymour Cray went on to form the spin-off Cray Computer Corporat" "ion (CCC), in 1989, which went bankrupt in 1995, while Cray Research was bought by SGI the next year... http://en.wikipedia.org/wiki/Cray"

Saturday, August 9, 2014

Friday, August 8, 2014

Print numbers w/ certain number of digits w/Sed

$echo -en "100\n10000\n34934\n3432\n" 
100
10000
34934
3432
put one less than the number of digits you want for the ending sequence:
$ echo -en "100\n10000\n34934\n3432\n" | sed -n '/^[0-9]\{1,2\}.$/p'
100
$ echo -en "100\n10000\n34934\n3432\n" | sed -n '/^[0-9]\{1,3\}.$/p'
100
3432
or if you only want 4 digits:
$ echo -en "100\n10000\n34934\n3432\n" | sed -n '/^[0-9]\{3\}.$/p'
3432
$ echo -en "100\n10000\n34934\n3432\n" | sed -n '/^[0-9]\{1,4\}.$/p'
100
10000
34934
3432

Sunday, August 3, 2014

Find and sort files by size in human readable format

find /tmp -maxdepth 10 -size +100000 -exec du -sh {} \; 2>/dev/null | sort -n -k 1
55M    /tmp/wolf/Sleep Music _ 1h of Wolf Relaxing Sounds _ Sounds Sleep Music #1-ArFicwCRupU.mp3
56M    /tmp/wolf/☽ Night Sounds with Wolves and Fire-KfkqfhN2tjA.mp3
127M    /tmp/wolf/☽ Night Sounds with Wolves and Fire-KfkqfhN2tjA.mp4
174M    /tmp/wolf/Sleep Music _ 1h of Wolf Relaxing Sounds _ Sounds Sleep Music #1-ArFicwCRupU.mp4
290M    /tmp/wolf/4hr Thunderstorm with No Loops 'Sleep Sounds'-ywBxqqpyfOc.mp4.part
 

or if you want, w/+ append is marginally quicker if you dont run out of resources

find / -maxdepth 10 -size +100000 -exec du -sh {} + 2>/dev/null | sort -n -k 1

Friday, August 1, 2014

Mingle multiple files with awk

$ echo -en {a..i}"\n"|sed 's/^[a-z]/ &/g;s/./& & & &/g'>file1;cat file1
       a a a a
       b b b b
       c c c c
       d d d d
       e e e e
       f f f f
       g g g g
       h h h h
       i i i i
$ echo -en {9..1}"\n"|sed 's/^[0-9]/ &/g;s/./& & & &/g'>file2;cat file2
       9 9 9 9
       8 8 8 8
       7 7 7 7
       6 6 6 6
       5 5 5 5
       4 4 4 4
       3 3 3 3
       2 2 2 2
       1 1 1 1

$ awk 'FNR==NR {a[FNR]=$0; next}{print a[FNR]"\n"$0}' file1 file2 
       a a a a
       9 9 9 9
       b b b b
       8 8 8 8
       c c c c
       7 7 7 7
       d d d d
       6 6 6 6
       e e e e
       5 5 5 5
       f f f f
       4 4 4 4
       g g g g
       3 3 3 3
       h h h h
       2 2 2 2
       i i i i
       1 1 1 1
$ awk 'FNR==NR {a[FNR]=$0; next}{print a[FNR] $0}' file1 file2 
       a a a a       9 9 9 9
       b b b b       8 8 8 8
       c c c c       7 7 7 7
       d d d d       6 6 6 6
       e e e e       5 5 5 5
       f f f f       4 4 4 4
       g g g g       3 3 3 3
       h h h h       2 2 2 2
       i i i i       1 1 1 1 
or for three or more files:  
$ awk 'FILENAME==ARGV[1]{a[FNR]=$0;next}FILENAME==ARGV[2]{b[FNR]=$0;next}FILENAME==ARGV[3]{print a[FNR] b[FNR] $0}' file1 file2 file3
       a a a a       9 9 9 9       1 1 1 1
       b b b b       8 8 8 8       2 2 2 2
       c c c c       7 7 7 7       3 3 3 3
       d d d d       6 6 6 6       4 4 4 4
       e e e e       5 5 5 5       5 5 5 5
       f f f f       4 4 4 4       6 6 6 6
       g g g g       3 3 3 3       7 7 7 7
       h h h h       2 2 2 2       8 8 8 8
       i i i i       1 1 1 1       9 9 9 9
 of course if thats all you need you could use: 
$ paste file1 file2 file3 
       a a a a        9 9 9 9        1 1 1 1
       b b b b        8 8 8 8        2 2 2 2
       c c c c        7 7 7 7        3 3 3 3
       d d d d        6 6 6 6        4 4 4 4
       e e e e        5 5 5 5        5 5 5 5
       f f f f        4 4 4 4        6 6 6 6
       g g g g        3 3 3 3        7 7 7 7
       h h h h        2 2 2 2        8 8 8 8
       i i i i        1 1 1 1        9 9 9 9
yet the awk way gives much more flexibility and possibilities