-1

Using sed command tried to match a value and then appending a value. Comd is working fine without variable but not able to pass the variable as expected.

eg:-This comd is working fine

sed -i 's/\( *OOZIEUSERS1 *\)[^ ]*\(.*\)*$/\1, test1,\2/' sudo_file.txt

but when running tru varible its not working:-

#!/bin/bash -x
read -p "Enter the reference account name : " ACC
read -p "Enter the user name to be inserted into VISUDO : " UNAME
sed -i  's/\( *$ACC *\)[^ ]*\(.*\)*$/\1, $UNAME,\2/' sudo_file.txt
zedfoxus
  • 32,227
  • 4
  • 59
  • 60
shaheer01
  • 101
  • 1
  • 11
  • use double quotes to expand the variable – Pankrates Dec 03 '15 at 06:58
  • Try using double quotes instead of single quotes with sed. `sed -i "s/\( *$ACC *\)[^ ]*\(.*\)*$/\1, $UNAME,\2/" sudo_file.txt`. Also see this answer: http://askubuntu.com/questions/76808/how-to-use-variables-in-sed-command – zedfoxus Dec 03 '15 at 06:58
  • http://stackoverflow.com/q/584894/1030675 – choroba Dec 03 '15 at 06:59

1 Answers1

0

You should use double quotes " if you want bash to expand your variables:

sed -i  "s/\( *$ACC *\)[^ ]*\(.*\)*$/\1, $UNAME,\2/" sudo_file.txt
Chris Maes
  • 30,644
  • 6
  • 98
  • 124
  • How do i change the command incase I want to make sure that UNAME gets added at the beginning of ACC instead of at the end. – shaheer01 Dec 03 '15 at 09:24
  • that's another question... first search around on stackoverflow if any answer exists; if not add another question with some more information: what is the output you get; what would you want to get, wat have you tried,... – Chris Maes Dec 03 '15 at 09:51