-1
#! /bin/csh

# NEW PASS value will change in every run
set NEW_PASS=`dyanamic generating password on everyrun`

echo $NEW_PASS

There is another conf file where we need to change the above generated password. In new.conf file corresponding line is pass=XYZ, Now above NEW_PASS value should be updated with new.conf file with replcing XYZ. I tried

sed -i -e "s/^pass*/pass=${NEW_PASS}/g" new.conf

But it's not working. Not sure if there is any issues with command syntax.

John
  • 75
  • Is the expression that you pass to sed single quoted or double quoted? Variables are not expanded in single quotes. There will be a syntax error if the variable contains slashes. Are you running this command on macOS? If on macOS, your use of -i and -e together will create a backup file called new.conf-e. Also, regular expressions are case sensitive, and pass* matches pas, pass, passs etc., but not the whole string pass= or anything thereafter on the same line (it also matches any line saying e.g. old_pass or that has any other prefix before pas). – Kusalananda Jun 17 '21 at 12:09
  • yes its with double quotes. sed -i -e "s/^pass*/pass=${NEW_PASS}/g" new.conf . is there any way we canupdate string after finding pattern (pass=) and how the special character will handle – John Jun 17 '21 at 12:29
  • If it's with double quotes, then it's not the command we see in the question, right? The command in the question has one double quote and one single quote. Consider updating your question to correct the command so that it's the same as what you are actually trying to use. Also let us know (in the question) what Unix you are using and, most importantly, exactly what happens when you run your command (a copy of any error messages etc.). It would be extra helpful to know the value of $NEW_PASS if possible, as some values of this variable are bound to cause problems. – Kusalananda Jun 17 '21 at 12:32
  • Now I updated the double quotes in sed. But its not updating pass=<value received from $NEW_PASS variable – John Jun 17 '21 at 12:41
  • 2
    @John The question still does not actually say what's happening (the "not working" bit). It's very difficult to diagnose a bug if all the details are not known. We need to know exactly what happens, and ideally also what the value of $NEW_PASS is (or at least if it works with some values and not others). – Kusalananda Jun 17 '21 at 12:43
  • 1
    You shouldn't try to do this with sed because you want a literal string replacement but sed doesn't understand literal strings (see is-it-possible-to-escape-regex-metacharacters-reliably-with-sed) and so will fail in cryptic ways depending on the contents of NEW_PASS. If you [edit] your question to show some concise, testable sample input and expected output (contents of new.conf before/after the desired command runs) then we can help you. – Ed Morton Jun 17 '21 at 12:52
  • Don't write scripts using csh by the way, it's a shell designed for interactive use, not writing scripts. For writing scripts use a bourne-derived shell like bash. Google "csh why not" for several articles on the subject. And don't use all upper case for non-exported variable names, see correct-bash-and-shell-script-variable-capitalization – Ed Morton Jun 17 '21 at 12:57
  • 1
    Unfortunately, after the edit it is still not clear how "it is not working". What is clear is that the syntax per se wouldn't do what you want, because you seem to use * as if it were a shell wildcard, whereas it has a very different meaning in regular expressions such as sed expects. – AdminBee Jun 17 '21 at 14:39

1 Answers1

0

It sounds like this is what you're trying to do:

$ cat new.conf
foo
pass=bar
etc

$ new_pass='this & "that, \t /foo \1'

$ new_pass="$new_pass" awk 'index($0,"pass=")==1{ $0 = "pass=" ENVIRON["new_pass"]} 1' new.conf
foo
pass=this & "that, \t /foo \1
etc

The value of new_pass I used is specifically chosen to highlight chars/strings that would be problematic if you used sed or even used awk in a different way (e.g. using -v instead of ENVIRON[] to pass in the shell variables value).

To modify the original file if you use GNU awk then do awk -i inplace 'script' new.conf; with other awks do tmp=$(mktemp) && awk 'script' new.conf > "$tmp" && mv "$tmp" new.conf`.

Ed Morton
  • 31,617