Saturday 15 September 2012

linux BASH - replacing text in a file

If you want to replace some text within a file, that is too large to open in a GUI application then, this is when you fall back on the command line tools like sed.

But to make the change permanent, you have to add -i to the command:

Code:   
Syntax:
sed -i 's/<old_word>/<new_word>/g' <file>
sed 's/<old_word>/<new_word>/g' input.txt > output.txt

sed -i 's/Hello/hello/g' input.txt
   

Sed can also use regular expressions, so it's a very powerful tool.

The above searches for 'Hello' and replaces it with 'hello'.

Thanks to Eric_T for this which, I put here as a reminder to myself.

Code:   
#!/bin/bash
OLD="xyz"
NEW="abc"
DPATH="/home/you/foo/*.txt"
BPATH="/home/you/bakup/foo"
TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f"
  else
   echo "Error: Cannot read $f"
  fi
done
/bin/rm $TFILE



A Note About Bash Escape Character
A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored). This is useful when you would like to deal with UNIX paths. In this example, the sed command is used to replace UNIX path "/nfs/apache/logs/rawlogs/access.log" with "__DOMAIN_LOG_FILE__":

Code:   
#!/bin/bash
## Our path
_r1="/nfs/apache/logs/rawlogs/access.log"

## Escape path for sed using bash find and replace
_r1="${_r1//\//\\/}"

# replace __DOMAIN_LOG_FILE__ in our sample.awstats.conf
sed -e "s/__DOMAIN_LOG_FILE__/${_r1}/" /nfs/conf/awstats/sample.awstats.conf  > /nfs/apache/logs/awstats/awstats.conf

# call awstats
/usr/bin/awstats -c /nfs/apache/logs/awstats/awstats.conf


The $_r1 is escaped using bash find and replace parameter substitution syntax to replace each occurrence of / with \/.

Thanks to Vivek Gite
http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

Very useful for replacing repetitive text string within a .csv file!

No comments: