misc-code2

=Misc. scripts, code excerpts, and scraps - Page 2=

tar_over_ssh
code format="bash" tar cvf - /lilly/data/tftpdir | ssh goombah "cat > /doug/tftpdir.tar"

code

Wildcards in sed
code format="bash" sed 's/From here.*to here/new text/g' code

Remove all HTML tags
code format="bash" sed -e :a -e 's/<[^>]*>//g;// d' temp.html > temp1.html

this works great, removing everything from line 1 until 

code

Grep and return additional lines in Solaris
code format="bash" nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

where "b" and "a" are the number of lines to print before and after string "s".

code

Grep multiple strings
code format="bash" egrep 'EMC|DGC' file or /usr/tools/emc/inq | egrep -e 'EMC|DGC' code

Print last 2 characters
code format="bash" Using awk: awk '{ print substr( $0, length($0) - 1, length($0) ) }' details.out Using sed: sed 's/^.*\(..\)$/\1/' details.out

code

Full-Featured Progress Bar
code format="bash"
 * 1) !/bin/bash
 * 2) progress-bar.sh


 * 1) Author: Dotan Barak (very minor revisions by ABS Guide author).
 * 2) Used in ABS Guide with permission (thanks!).

BAR_WIDTH=50 BAR_CHAR_START="[" BAR_CHAR_END="]" BAR_CHAR_EMPTY="." BAR_CHAR_FULL="=" BRACKET_CHARS=2 LIMIT=100

print_progress_bar {       # Calculate how many characters will be full. let "full_limit = ((($1 - $BRACKET_CHARS) * $2) / $LIMIT)"

# Calculate how many characters will be empty. let "empty_limit = ($1 - $BRACKET_CHARS) - ${full_limit}"

# Prepare the bar. bar_line="${BAR_CHAR_START}" for ((j=0; j<full_limit; j++)); do               bar_line="${bar_line}${BAR_CHAR_FULL}" done

for ((j=0; j<empty_limit; j++)); do               bar_line="${bar_line}${BAR_CHAR_EMPTY}" done

bar_line="${bar_line}${BAR_CHAR_END}"

printf "%3d%% %s" $2 ${bar_line} }

MAX_PERCENT=100 for ((i=0; i<=MAX_PERCENT; i++)); do       # usleep 10000 # ... Or run some other commands ... #       print_progress_bar ${BAR_WIDTH} ${i} echo -en "\r" done
 * 1) Here is a sample of code that uses it.

echo ""

exit code

Log File Analysis
code format="bash" awk '{ s += $1 } END { print "sum: ", s, " average: ", s/NR, " samples: ", NR }' whatever.log

code

Generate Random #s
code format="bash" Random # between 1 and 4 $(($RANDOM%4+1))

code

Add Leading Zeros
code format="bash" NUMBER=$(printf %02d $NUMBER) - Add 1 leading zeros - 3 becomes 03, 12 becomes 12 NUMBER=$(printf %03d $NUMBER) - Add 2 leading zeros - 3 becomes 003, 12 becomes 012 code