0

I have two files:

File1:

not_keyword: 'something'
keyword: 'condition'
another_not_keyword: 'something'

File2:

condition 1 condition 2 condition 3

I would like to find all occurrences of keyword in File1 and replace its content (condition) with the content of File2.

Desired output in this case:

not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something"
  • Should the non-occurrences of keyword in File1 be kept or should they be removed? – Paulo Tomé Jan 18 at 23:45
  • Good question @PauloTomé! I've edited the original question. They should be kept. TY! – t988GF Jan 19 at 0:36
3

How about

$ awk -F': ' '
    BEGIN{OFS=FS; getline value < "File2"} $1 == "keyword" {$2 = q value q} 1
  ' q="'" File1
not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something'
|improve this answer|||||
  • Works perfectly! Any change the changes can be done directly in the file itself? – t988GF Jan 19 at 1:34
  • @t988GF if you have a sufficiently recent version of GNU awk (gawk) then yes - by adding -i inplace to the command line options – steeldriver Jan 19 at 1:52
  • Good call. Turns out I'm running on a system where I can't install anything. gawk seems recent enough but unfortunately I won't be able to use it. I'll mark the answer as correct, since it does in fact do what I need, even though if someone could provide an answer with standard coreutils I would appreciate it. – t988GF Jan 19 at 13:50

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.