Juan Garcés

Personal Blog

Replace With VIM

August 17th, 2014

Many times I have seen the need to replace text with VIM. Here I leave the way to do.

First, we open the file to edit:


vim file

We press ESC and insert colon “:”. After that, we insert the instruction:


:1,$ s/search_text/replacement_text/g

The characters 1,$ indicate that should search from the first to the last line of the file. After, just replace “search_text” and “replacement_text” for that we need.

Connect MySQL with PDO

August 17th, 2014

Connection to MySQL using PDO library.

CREATE TABLE `paciente` (
  `paciente_rut` varchar(12) NOT NULL,
  `paciente_nombre` varchar(100) NOT NULL,
  `paciente_apellidop` varchar(100) NOT NULL,
  `paciente_apellidom` varchar(100) default NULL,
  `paciente_fono` varchar(50) default NULL,
  `paciente_direccion` varchar(255) NOT NULL,
  PRIMARY KEY  (`paciente_rut`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `paciente` VALUES ('11111111-1', 'Pedro', 'Fuentes', 'García', NULL, 'Santiago');
INSERT INTO `paciente` VALUES ('22222222-2', 'Juan', 'Santana', 'Bustos', NULL, 'Santiago');
INSERT INTO `paciente` VALUES ('33333333-3', 'Carolina', 'Soto', 'Barra', NULL, 'Santiago');

Crypt with SHA1

August 9th, 2014

Method to crypt with SHA1

/// 
/// Crypt the string with SHA1
/// 
public static string EncriptarSHA1(string sCadena)
{
	System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1Managed.Create();

	ASCIIEncoding encoding = new ASCIIEncoding();
	byte[] stream = null;
	StringBuilder codificada = new StringBuilder();
	stream = sha1.ComputeHash(encoding.GetBytes(sCadena));

	for (int i = 0; i < stream.Length; i++) codificada.AppendFormat("{0:x2}", stream[i]);
	return codificada.ToString();
}

Arrays with SPL

August 9th, 2014

Using SPL to work with an array.

/* Arreglo */
$personas = array("Carlos", "Carolina", "José",
                  "Manuel", "Sandra");

try
{
	/* Creamos el objeto SPL */
	$arrayObjSPL = new ArrayObject($personas);

	/* Modificamos el elemento 4 */
	$arrayObjSPL->offsetSet(4, "Claudia");

	/* Eliminamos el elemento 2 */
	$arrayObjSPL->offsetUnset(2);

	/* Iteramos sobre el arreglo con el objeto SPL */
	for($iterator = $arrayObjSPL->getIterator();	$iterator->valid();	$iterator->next())
	{
		/* Mostramos el elemento */
		echo $iterator->key() . " -> " . $iterator->current() . "
"; } } catch (Exception $e) { echo $e->getMessage(); }

C# isNumeric

August 9th, 2014

Visual Basic IsNumeric method for C#.


/// Check if obj is numeric
public static bool IsNumeric(object obj)
{
	double retNum;
	bool isNum = Double.TryParse(Convert.ToString(obj),
								 System.Globalization.NumberStyles.Any,
								 System.Globalization.NumberFormatInfo.InvariantInfo,
								 out retNum);
	return isNum;
}

Mails with mailx

March 17th, 2013

With this function you can send mails using mailx command.

#!/bin/sh

send_email()
{
  #
  # Proceso de parámetros
  #
  DIRECCION="$1"
  ASUNTO=""
  MENSAJE=""
  if [ $# -ge 2 ]; then
    if [ "$2" != "-" ]; then
      ASUNTO="$2"
    fi
  fi
  if [ $# -ge 3 ]; then
    if [ "$3" != "-" ]; then
      MENSAJE="$3"
    fi
  fi
  ADJUNTO=""
  if [ $# -ge 4 ]; then
    if [ "$4" != "-" ]; then
      ADJUNTO="$4"
    fi
  fi
  #
  # Envío del email
  #
  ( echo "$MENSAJE";
    if [ "$ADJUNTO" != "" ]; then
      archivo=`echo $ADJUNTO|cut -d'~' -f1`
      nombre=`echo $ADJUNTO|cut -d'~' -f2`
      if [[ "$archivo" == "$nombre" || "$nombre" == "" ]]; then
        nombre=`basename $archivo`
      fi
      if [ -f "$archivo" ]; then
        uuencode "$archivo" "$nombre";
      fi
    fi;
  ) |  mailx -s "$ASUNTO" "$DIRECCION"
}

send_email "yourmail@yourdomain.com" "Subject" "Message body" "attachment.txt"

Welcome

January 1st, 2013

Welcome!

In this blog I will share with you my experiences on different tools that I have worked, as well as developments and personal studies.

All content is free to access and you can be used without restrictions. Check the Terms of Use

Regards

Juan Garcés Bustamante

Juan Garcés

Personal Blog