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.

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"

Juan Garcés

Personal Blog