linux shell script sed with regexp expression examples

Started by ggnfs000, January 14, 2017, 06:02:30 AM

Previous topic - Next topic

ggnfs000

linux shell script sed with regexp expression examples

Simple regexp expression using sed. In this examples, grep output is piped to sed. Sed in turn looks for .*= expression and replaces with empty. /s signifies substitution operation. Resulting value is fed into MOUNT_USER[$i] variable.
This is most commonly used cases for me:

MOUNT_USERS[$i]=`cat $PASSWORD_FILE | grep ${MOUNT_USERS_LABELS[$i]} | sed -r 's/.*=//'`

Following examples shows the replacement of every occurrence of \. and replaces with - in var2 and result is fed into var1. Notice that the var2 is the variable holding the name of file which is output using cat command which is piped into sed expression. Last part of expression '/g' signifies that it wants to replace every occurrence.
 
var1=`cat $var2 | sed -r 's/\./-/g'`
 




Source: linux shell script sed with regexp expression examples