Sed in Linux

sed is stream   editor, which means that you can find and replace any strings or multiple strings using sed command. It is just like similar as vi find and replace  command . But sed is used to execute that multiple find and replace  commands at once using script.

Using command on terminal.

sed -i  's/john/JOHN/g'  report.tex.

To replace multiple strings

sed -e ‘s/john/JOHN/g’  -e  ‘s/mariya/MARIYA/g’  report.tex.

Following command will show output on terminal

 sed -f replace.sed  report.tex

Script to replace multiple strings.

To do this, create two files. one with .sed extension and second with .sh. Because its a bash script file. And third file will be your source file , on which you want to apply script.

replace.sed
s/john/JOHN/g
s/aliya/ALIYA/g
s/cherry/CHERRY/g
s/mariya/MARIYA/g


script.sh
#!/bin/bash
 sed -f replace.sed < report.tex > correctreport.tex

Output: after running script.sh
\documentclass{report}

\begin{document}
\date
\author{Manpreet Dhiman}
\maketitle
\chapter{student record}
\section{name}

 \begin{enumerate}
  \item JOHN
  \item MARIYA
  \item sid
  \item ALIYA
  \item CHERRY
\end{document}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s