Pages

Monday, August 11, 2014

SED Search and Replace



I am using /etc/nsswitch.conf file in below format  as and example.

# cat  /etc/nsswitch.conf

passwd:         compat
group:          compat
shadow:         compat

hosts:          files
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis


1. How to replace pattern with another using sed ?




# sed 's/netgroup/netgroup1/' /etc/nsswitch.conf


2. How to replace word with another  in line contain keyword using sed ?

Below example will be replacing word files with ldap if the line contain the word  services.

# sed  '/services/s/files/ldap/' /etc/nssswitch.conf

GLOBAL FLAG


3 . How to replace the 1st occurrence of lower case "a" with upper case "A" ?

#  sed 's/a/A/'  /etc/nsswitch.conf


4. How to replace all occurrences of lower case "a" with upper case "A" ?


# sed 's/a/A/g'  /etc/nsswitch.conf



Number Flag



5. How to replace the 2nd occurrence of lower case "a" to upper case "A:" ?

#  sed 's/a/A/2' /etc/nsswitch.conf







Saturday, January 11, 2014

Customizing Your SHELL Prompt in Unix with TIME



How to display time in the bash promt ?



# declare -x PS1=" \@ Bash $ "


 05:05 PM Bash $

Setting Permission while creating directories

How to set permission while creating creating directory in linux ?

# mkdir --parents --mode=550 vasanth/backup/january

--mode=m ( -m )—Sets the permission mode (as in chmod )
--parents ( -p )—Makes all necessary directories even if they don’t currently exist



How to classify files and directories in "ls" command ?


How to classify in ls command ?


ls command can classify directories,files,programs,symbolic links and named pipes.



# ls --color=never --classify


The –classify symbols are directories ( / ), programs ( * ), symbolic links ( @ ), pipes ( | ),and Unix domain socket files ( = ).






OR & AND conditions in shell scripts

OR Statement in shell scripts

|| is known as OR statement in shell script.

Eg:


# id user1 || useradd user1

In the above example if the user1 exist in the system the second command will not be executed. If the user1 does not exist second command will be executed.

If first command returns true second will not exist run. if the command is false second will be executed.

AND Statement in shell scripts


 Eg:

&& is known as AND in shell script


#  [ -d /tmp/test ] && rm -rf /tmp/test

 If the first command is true the second command will be executed. If the first
false second command second will not executed.