Organizational Research By

Surprising Reserch Topic

Question:how to use sed unix command on command line?


sed

Sed is a very powerful “stream-oriented” editor. In conjunction with regular expressions and the find command, you can perform some complex search and replace functions across a set of files. Consider a file named “testing.txt” which contains the text “This is only a test”. The following command would alter the word “test” to “disaster”.


sed -i '' s/test/disaster/g testing.txt

The -i flag means “edit in place”. You may optionally pass a parameter to this flag which will make a backup copy of the file(s) you are modifying and append the extension you pass it. Consider our original file and original content; the following command will replace the word “test” for “disaster” and will make a copy of the original file called “testing.txt.orig” in its untouched state.


sed -i '.orig' s/test/disaster/g testing.txt

Sed is an extremely powerful editor, and it can modify many files simultaneously when combined with the ‘find’ command from above. The following command will search through the current directory and all subdirectories and replace the string “http://” with the string “https://” in each html file.


find . -type f -name '*.html' -exec sed -i 's/http\:\/\//https\:\/\//g' {} \;

Notice that certain characters must be escaped in the search parameters of sed. The curly braces “{}” pass each filename returned from ‘find’ as an argument into ‘sed’ and the “\;” is required by exec as a termination character. This is only one small sample of what sed can do and I highly recommend reading the sed man page to learn more options.

asked Sep 13, 2013 in LINUX by anonymous
edited Sep 12, 2013
0 votes
15 views



Related Hot Questions



Government Jobs Opening


...