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








awk


command – awk ‘{print $1}’ 
cat dog horse
              lion tiger bear

The output of the same command is   cat
                                                        lion

myFile contains data separated by a : e.g. cat:dog:horse
awk –F “:” ‘{print $1}’ myFile.
will print as normal again



ls –l | awk '{print $9}'

                                    file1
                                    file2
                                    file3    etc.
will print file names

ls –l | awk '{bytes += $5; print $9 "  " $5 " "$3 " Total " bytes " bytes"}'


implement something once at the start or end of the command. For example:
ls –l |awk '
{bytes += $5;
print $9 "  " $5 " " $3}’
END{print "Total " ttl " bytes"}'

ls –l | awk '
BEGIN{print"Directory Listing"}
{print $9 " "$5}'

ignoring lines
ls –l | awk '
BEGIN{print " Directory Listing"}
{if($1 == "total") next;
print $9}
END{print "Thank you and Goodbye"}'





groups


Use the useradd command to create three new users bill, kate and jane e.g.

useradd jane                                 ccreate user

            passwd jane                                  give password

 cat /et/cpasswd and cat /etc/shadow.                     check users where created

 su jane.                 log on as another user


Home Directories

            mkdir jane      make directory

            ls –l                                        show details on current dir


            chown jane jane        change owner

            chgrp users jane        change group



Groups

To create a new group:           
           
            groupadd myGroup

To add users to this group

            groupmod –A jane myGroup (do this for two users)
  cat /etc/group opens group file


reads out a file
while read names
     do
              echo $names
     done < fileName