Home > Mobile >  WHOAMI in SED command
WHOAMI in SED command

Time:07-19

whoami|cut -c5,6,7----gives dev

sed -e 's@man[a-zA-Z]*@manwhoami|cut -c5,6,7@g' file_name.txt
##whoami|cut -c5,6,7 is in between ``

with this I am geting manwhoami|cut -c5,6,7 instead of mandev in the file.

file_name.txt LogFile=/manrun/in/get/new1/

after I run $sed -e 's@man[a-zA-Z]*@manwhoami|cut -c5,6,7@g' file_name.txt

I am geting as below: $cat file_name.txt LogFile=/manwhoami|cut -c5,6,7/in/get/new1/ I need LogFile=/mandev/in/get/new1/

any inputs plz.

CodePudding user response:

If you put your stuff inside '' then it will not be evaluated.

To evaluate a command you put it as $(command)

I suggest you to store the command result into a variable and then reference the variable in the sed expression.

CodePudding user response:

As pointed by Lucas Eduardo, try this:

#!/bin/bash
BASE=$(whoami|cut -c5,6,7)
sed -e "s@man[a-zA-Z]*@man${BASE}@g" file_name.txt

In the comments you have mentioned it did not work for you, that is because you were using single quotes and thus the substitution was not made. Notice in my code I use double quotes and so the substitution will occur.

Hope this helped you!

  • Related