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"
2
  • Should the non-occurrences of keyword in File1 be kept or should they be removed? Commented Jan 18, 2020 at 23:45
  • Good question @PauloTomé! I've edited the original question. They should be kept. TY!
    – t988GF
    Commented Jan 19, 2020 at 0:36

1 Answer 1

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'
3
  • Works perfectly! Any change the changes can be done directly in the file itself?
    – t988GF
    Commented Jan 19, 2020 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 Commented Jan 19, 2020 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
    Commented Jan 19, 2020 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .