Pages

Text Replacement through bash script

Normally, in Code-Projects, we write generic codes with hard coaded Variables whose values are known at the deployment time. For example, we may write START_PORT, STOP_PORT in pom.xml and these ports may very as per the environments are being deployed on.
Linux based build system have Makefile, where you type something like
make init profile=dev && make run profile=dev
and as per the profile supplied, the internal values in the project has to adjust.
One way of doing it is by using the linux sed command. sed is a command for editing io-streams.
Example:
Create a file named file1.txt
Write some content. (My name is <Name>)
Now with a command prompt, type
cat file1.txt |sed s/""/Rajan/g |cat > file2.txt
First check the content of file2.txt.
Now let me explain you what the command is doing.
First the cat command in left extracts the content of file1.txt and gives it to sed command. The sed command is taking an input stream and replacing the pattern “” with “Rajan” text. Not the stream is again piped to cat command as an input which outputs to the file2.txt file.

No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...