Using variables in sed -f (where the sed script is in the file, not in the line)

We have a process that can use a file containing sed commands to change input in a pipe.

I need to replace the input placeholder with a variable with a value, for example. in one type of -e command that I can run;

 $ echo "Today is XX" | sed -e "s/XX/$(date +%F)/" Today is 2012-10-11 

However, I can only specify aspects of sed in the file (and then specify the process in the file), for example. a file called replacements.sed may contain:

 s/XX/Thursday/ 

So obviously

 $ echo "Today is XX" | sed -f replacements.sed Today is Thursday 

If I want to use an environment variable or shell value, I cannot find a way to expand it, for example. if replacements.txt contains;

 s/XX/$(date +%F)/ 

Then;

 $ echo "Today is XX" | sed -f replacements.sed Today is $(date +%F) 

Including double quotes in the text of the file simply prints double quotes.

Does anyone know how to use variables in a sed file?

+4
source share
4 answers

This may work for you (GNU sed):

 cat <<\! > replacements.sed /XX/{s//'"$(date +%F)"'/;s/.*/echo '&'/e} ! echo "Today is XX" | sed -f replacements.sed 

If you do not have GNU sed, try:

 cat <<\! > replacements.sed /XX/{ s//'"$(date +%F)"'/ s/.*/echo '&'/ } ! echo "Today is XX" | sed -f replacements.sed | sh 
+7
source

AFAIK, this is not possible. Your best bet would be:

INPUT FILE

 aaa bbb ccc 

SH SCRIPT

 #!/bin/sh STRING="${1//\//\\/}" # using parameter expansion to prevent / collisions shift sed " s/aaa/$STRING/ " " $@ " 

TEAM LINE

 ./sed.sh "fo/obar" <file path> 

OUTPUT

 fo/obar bbb ccc 
+3
source

As others have said, you cannot use variables in a sed script, but you can "fake" it using additional leading input, which is added to your storage buffer. For instance:

 [ ghoti@pc ~/tmp]$ cat scr.sed 1{;h;d;};/^--$/g [ ghoti@pc ~/tmp]$ sed -f scr.sed <(date '+%Y-%m-%d'; printf 'foo\n--\nbar\n') foo 2012-10-10 bar [ ghoti@pc ~/tmp]$ 

In this example, I use process redirection to get input to sed. "Important" data is generated by printf . Instead, you can write a file or run some other program. The "variable" is created by the date command and becomes the first line of script input.

The sed script command takes the first line, puts it in the sed storage buffer, and then deletes the line. Then, for any subsequent line, if it matches a double dash (our "macro replacement"), it replaces the contents of the hold buffer. And prints because this is the default sed action.

Holding buffers (g, g, h, H, and x commands) is an advanced gray programming. But once you understand how they work, they open up new dimensions for sed fu.

Note. This solution will only help you replace whole lines. Replacing substrings within strings may be possible using a hold buffer, but I cannot imagine how to do this.

(Another note: I am doing this on FreeBSD, which uses a different sed from what you find on Linux. This may work in GNU sed, or it may not be so: I have not tested it.)

+2
source

I agree with sputnick. I do not believe sed will be able to complete this task.

However, you can generate this file on the fly.

You can change the date to a fixed string, for example __DAYOFWEEK__ .

Create a temporary file, use sed to replace __DAYOFWEEK__ with $(date +%Y) .

Then sed -f $TEMPFILE file with sed -f $TEMPFILE .

sed is fine, but maybe it's time to use something like perl that can generate a date on the fly.

0
source

Source: https://habr.com/ru/post/1439050/


All Articles