Useful 'sed' examples

1. Replace the first line of a file with a string

     sed -i '1 s/^.*$/string/' FILE

2. Replace an entire line containing a string with string2


     sed -i '/string/c\string2' FILE

                        0r


     sed -i 's/.*string.*/string2/g' FILE

3. Remove an entire line containing a string
   
     sed -i '/string/d' FILE

4. To replace a word with word2

     sed -i 's/word/word2/g' FILE

5. Create a backup first before replacing the word

    sed -i.bak 's/word/word2/g' FILE

Comments

Popular posts from this blog

MySQL table conversion

Add addon-domain, sundomain and parked domains via shell in a Cpanel server

Cpanel Migration Script