Linux Tips and Tricks

Saturday, May 13, 2006

Bash/Sed implementation of PHP add_slashes function

This script will read lines from stdin end replace the ' with \' This is perfect for parsing text files and composing SQL queries.

#!/bin/bash

#filename: add_slashes.sh

read line
while [[ "$line" != "" ]]
do
echo "$line" | sed "s/'/\\\\'/g"
read line
done

Usage example: echo "Heherson's dog" | ./add_slashes.sh will produce "Heherson\'s dog"

1 Comments:

  • Nice one. Very useful. Thanks!

    By Anonymous Anonymous, at 8:41 AM  

Post a Comment

<< Home