sed is a special editor modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use.

Now, I introduce the most basic command that called substitution(s). The other commands will be introduced in other articles.

1.sed s/day/night/ day_file

There are four parts of this command.

s: substitute command

/../../: delimiter

day: regular expressiojn pattern for searching

night: replace string

This command will replace the first 'day' with 'night' line by line and show the results to the std. Attention: this command will not change the day_file.

2.sed s/day/night/g day_file

This command will replace every 'day' with 'night' line by line and show the results to the std. Attention: this command will not change the day_file. And g means global.

3.sed s/day/night/g < day_file > night_file

This command will replace every 'day' with 'night' line by line and generate the night_file to store the result. Attention: this command will not change the day_file.

4.sed s/day/night/g day_file > day_file_bak; mv day_file_bak day_file

This command will replace every 'day' with 'night' line by line and make changes to the day_file.(In fact, the command generate a new file named 'day_file_bak' to store the results and rename the 'day_file_bak' to 'day_file'. 

posted on 2012-09-28 12:19  Neoh  阅读(414)  评论(0编辑  收藏  举报