-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
  • 35,121
  • 5
  • 64
  • 63
shaheer01
  • 111
  • 2
  • 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
  • 35,025
  • 12
  • 111
  • 136
  • 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