Search and Replace Text
Find and replace all instances of a string in all files in current (and any subs) directory.
Posts tagged ‘Hacks’
Find and replace all instances of a string in all files in current (and any subs) directory.
while looking through my snippets for a script to parse a simple “several fields to a line, one record to a line” type of data set, I realized I was probably over engineering the solution, the following is the result of a few whiles playing with a few ideas:
#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
echo -------------
echo ${LEN}
echo "post:"${ITEMS}
#get the shortest match from front of the source string
# by matching the longest from the back of the source string
THIS_FIELD=${ITEMS%%;*}
#remove the match from the source string
ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
#calc the length of the source string
LEN=${#ITEMS}
#show results
echo "anno:"${ITEMS}
echo ${THIS_FIELD}
done
will output:
the idea can be taken further by using an array:
#!/bin/bash
#declare the array
declare -a FIELDS
ITEMS='field 1;field 2;field 3;field 4;field 5'
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
echo -------------
echo ${LEN}
echo "post:"${ITEMS}
#get the shortest match from front of the source string
# by matching the longest from the back of the source string
THIS_FIELD=${ITEMS%%;*}
#remove the match from the source string
ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
#calc the length of the source string
LEN=${#ITEMS}
#store result in the array
FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
#show results
echo "anno:"${ITEMS}
echo ${THIS_FIELD}
echo ${FIELDS[ (${#FIELDS[*]}-1) ]}
done
echo -------------
#show the array count
echo ${#FIELDS[*]}
#show the array content
echo ${FIELDS[*]}
will output:
the final script fragment without the comments::
#!/bin/bash
ITEMS='field 1;field 2;field 3;field 4;field 5'
declare -a FIELDS
LEN=${#ITEMS}
while [ "$LEN" -gt "0" ]; do
THIS_FIELD=${ITEMS%%;*}
ITEMS=${ITEMS:$((${#THIS_FIELD}+1))}
LEN=${#ITEMS}
FIELDS[ (${#FIELDS[*]}) ]=${THIS_FIELD}
done
COUNT=0
while [ "$COUNT" -lt ${#FIELDS[*]} ]; do
echo ${FIELDS[$COUNT]}
COUNT=$(( ${COUNT}+1 ))
done
obviously you would want to change the “ITEMS” variable and do something meaningful in the second loop.
this was posted in the hope that it will be of some use to someone.
I had a postscript document that simply did not want to come out correctly, the offsets were such that the page content was going well off the visible area.
the following vile hack is one solution:
cat <<EOF>offsetfix.ps %!PS-Adobe-2.0 << /PageOffset [ 16 -52] /Margins [0 0] /.HWMargins [0 0 0 0] >> setpagedevice EOF gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite \ -dOptimize=true \ -sOutputFile=output.pdf \ offsetfix.ps input.ps
granted, one could cat the snippit in offsetfix.ps with the original document, but as I’m not sure what else may be using it, I’d rather not.
this also has the advantage of being a bit more portable, albeit rather ugly when compared to the original:
ps2pdf input.ps output.pdf
function get_post_content($id) {
global $wpdb;
$query =
" SELECT ID, post_title, post_excerpt, post_content, post_content_filtered "
."FROM $wpdb->posts "
."WHERE ID = '$id'";
$pages = $wpdb->get_results($query);
if( ! empty($pages) && isset($pages[0]) ) {
//echo( 'Title: '.$pages[0]->post_title."\n" );
//echo( 'ID: '.$pages[0]->ID."\n" );
return $pages[0]->post_content;
}
return '';
}
note that this method does not distinguish between posts that have or have not been published, this allows the content of an unpublished post to be used eleswhere.
insert the following in one of your templates:
<?php
if( function_exists(’insert_ie_fixes’) ) {
insert_ie_fixes();
}
?>
I put it at the bottom of footer.php to make sure it overrides prior declarations, but it may work in header.php as well.
then add the following to functions.php in the theme you are using:
function insert_ie_fixes() {
?>
<!–[if IE]>
<style>
RULES SPECIFIC TO IE GO HERE
</style>
<![endif]–>
<?php
}
this could also be used to insert general styles or scripts.
Change File Extention
Add a user for server/system procs
Change the owner of just the dirs in the current tree
Change the owner of just the files in the current tree
Change the permissions of just the dirs in the current tree
Change the permissions of just the files in the current tree
Change the permissions of files and dirs in the current tree