1

I need to use sed to retrieve a variable form within a file name. I am updating a previous code:

cat /directory/filename1.xml | sed -e "s/XYZABC/${c}/g" > /directory/filename2.xml

The value for ${c} is in another file and it will change each day. How do I do this with sed?

Here is some additional context. I apologize for failing to include it originally. ${c} is a sqlplus variable used to store the count of a query. I need to retrieve that count from a file. It is saved in the file as Count=[$c] where [$c] becomes the count from the query. I need to have $c replace XYZABC from the first xml file and save the results into a new xml file.


UPDATE: In case anyone has a similar issue I got mine to work using:

rm /directory/folder/variablefilename.dat
echo $c >> /directory/folder/variablefilename.dat

d=$(grep [0-9] /directory/folder/variablefilename.dat)

sed -3 "s/ABC123/${d}/g" /directory/folder/inputfile.xml >> /directory/folder/outputfile.xml
2
  • the whole file is $c? or only a part? if the latter, what are the criteria for determining what is $c and what isn't? – mikeserv Dec 30 '15 at 21:31
  • 1
    please edit any relevant information into the question. consider providing code-block formatted i/o examples - they are generally the most effective means of conveying your purpose. – mikeserv Dec 30 '15 at 21:54
1

Normally if you want to use sed (or perl, or ruby) to modify a file, you add the -i option. Let's say we're talking an attribute assignment in XML:

changeit() {
  let newval="$1"
  shift; 
  # "$@" now contains all filenames
  sed -i -e 's/(XYZABC[[:space:]]*=[[:space:]]*)"([^"]*)"/\1"'"$newval"'";/' "$@"
}

changeit happy-happy /directory/filename1.xml

Before you test, please make a backup copy of the directory/files and do diffs afterwards. sed -i can be very destructive.

Note, this applies the substituion once per line. See other questions / answers for once per file. For multiple times per line, add g after the last slash (/).

An explanation of the quoting rules: I'm assuming well-formed XML encapsulates attribute values in double-quotes. So we're looking for VARNAME = "something". This works as long as something doesn't itself contain an escaped double-quote character. So we have to keep the double-quotes from the shell, and for that we using single-quotes. In doing so, we prevent the variable $newval from being interpolated. So we have to close off the quoted part of the command, just before the variable expansion, then expand $newval, which should now be double-quoted to prevent shell from thinking embedded spaces start a new argument (and therefore filename) to sed, and then resume single-quoting so we can replace the double-quotes as part of sed's output.

1
  • ... and I just read comments to your question. facepalm – Otheus Dec 30 '15 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.