0

How do I change multiple words using sed? For example, I have a plain text file with the contents being

Var_A = 1
Var_B = 2
Var_C = 3

How do I change 1, 2 and 3 to AAA, BBB and CCC so that the resulting file looks like this:

Var_A = AAA
Var_B = BBB
Var_C = CCC

I've tried using this

sed -i 's/Var_A = 1\nVar_B = 2\nVar_C = 3/Var_A = AAA\nVar_B = BBB\nVar_C = CCC' file.txt

but it does not work at all.

6
  • 1
    Welcome to the site. It is likely that sed is not the right tool for the purpose, but we would need more details. In particular: where do you define the mapping between 1 and AAA etc.? Is it stored in a file? Please don't post requested details in the comment section ,but edit your question to add them so that all contributors have the complete picture at a glance.
    – AdminBee
    Jul 27, 2021 at 15:46
  • I have a random .txt file with the only contents being ` Var_A = 1 Var_B = 2 Var_C = 3 ` I was hoping if there was a way to replace 1,2,3 in that .txt file
    – Geralt
    Jul 27, 2021 at 15:52
  • 2
    I understand that, but is there a second file that says "1 should be replaced by AAA" and "2 should be replaced by BBB" etc.? If not, how do you want to specify that mapping? Is it all part of a larger shell script where you have that stored as shell variables? The problem in understanding your question is that the way you currently describe it, it sounds easier to simply rewrite the file rather than try to edit it, so I'm pretty sure we are still missing details here ;)
    – AdminBee
    Jul 27, 2021 at 15:53
  • There's not specific mapping here. All I'm trying to do is replace multiple words but because those words have a new line between them I'm not able to accomplish that through the script I posted. Going forward, I want to setup a script I could use (this is a very simply use-case that can be used for lot of purposes) instead of manually opening the file and making edits (why would I want to rewrite a file? let say my txt file has millions lines of codes rewriting that file makes no sense).
    – Geralt
    Jul 27, 2021 at 16:03
  • 2
    Of course you don't actually want to rewrite your file. I get that. The problem is that from your description it doesn't become clear what the best solution for your use case actually is, because while you certainly don't want to rewrite a file with millions of lines of code, you will also probably not want to compose a sed command with millions of individual substitution arguments. That is why I was asking for more details.
    – AdminBee
    Jul 27, 2021 at 16:11

4 Answers 4

5

It would seem that you are looking for a basic test case on how to use sed for replacing text. In your case, I would propose the following command:

sed -e '/^Var_A/s/1/AAA/' -e '/^Var_B/s/2/BBB/' -e '/^Var_B/s/3/CCC/' file.txt

This will apply three "targeted" substitution commands that will check if a line starts with Var_A, and if so, replace the 1 with AAA (and so on).

3

Let's see if something like this is what you are looking for:

# define the replacement variables
$ a='AAA';b='BBB';c='CCC'
# make each replacement using the variables.
$ sed -e "s/^\(Var_A = \).*$/\1$a/" -e "s/^\(Var_B = \).*$/\1$b/" -e "s/^\(Var_C = \).*$/\1$c/" foo.txt 
Var_A = AAA
Var_B = BBB
Var_C = CCC

Since the purpose of sed is process text line by line, it's not usual or at least a good idea to try to match new lines.

0
1

Taking the question literally without trying to figure out what is actually happening:

$ cut -d ' ' -f -2 file | paste -d ' ' - <( printf '%s\n' AAA BBB CCC )
Var_A = AAA
Var_B = BBB
Var_C = CCC

This discards the values after the equal signs in the original file using cut, and then attaches new values produced by printf using paste.


Using sed (and printf and paste):

printf '%s\n' AAA BBB CCC | paste -d @ file - | sed 's/\(= \).*@/\1/'

This produces the complete original file and the new data side by side with the two sets delimited by @ characters like so:

Var_A = 1@AAA
Var_B = 2@BBB
Var_C = 3@CCC

sed is then used to replace everything between = and @ on each line with = .

0

In case you want to use the arrays, the following solution may be of use:

$ v=([1]=AAA BBB CCC)
$ cat <<. | sed -e "s/1/${v[1]}/
> s/2/${v[2]}/
> s/3/${v[3]}/"
> Var_A = 1
> Var_B = 2
> Var_C = 3
> .
Var_A = AAA
Var_B = BBB
Var_C = CCC

You can modify the search part to the variables of choice and then, substitute.

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.