Thursday 7 March 2013

sed


  • s means "substitute" or search and replace.
  • g which means "replace all matches"

Consider the following file fileEg01 which contains a single line of text

I own a coat

Issue the command    sed -e 's/coat/bag/g' fileEg01

will display   I own a bag 


sed -e 's/coat/bag/g' fileEg01 >> fileEg03
saves the new replacement

two replacements
sed -e 's/bag/dog/g' -e 's/coat/cat/g' fileEg02

I own two dogs and one cat


The following is a simple example sed -e '1,3d' pass2 - this deletes the first 3 lines of the file pass2.

The command sed -e '2,3d' pass2 deletes the second and third lines.

The command sed -e '5,$d' pass2 deletes line 5 and everything else to the end of the file.

Deleting all members of a group from the passwd file.

Consider the selection from the /etc/passwd file. Note that the first three users jane, mary and bob belong to a different default group from the others.

jane:x:1004:1000:Jane:/home/jane:/bin/bash
mary:x:1005:1000:mary:/home/mary:/bin/bash
bob:x:1006:1000:bob:/home/bob:/bin/bash
miley:x:1007:100::/home/miley:/bin/bash
dick:x:1008:100:dicky:/home/dick:/bin/bash
joe:x:1011:100:joey:/home/joe:/bin/bash








No comments:

Post a Comment