Skip to main content
extended solution with additional sed approach
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Awk approach:

awk '$1=="keyword:"{ getline k < "file2"; print $1, "\047" k "\047"; next }1' file1

The output:

not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something'

As an alternative use sed in-place substitution:

$ sed -Ei "s/^(keyword: ).*/\1'$(cat file2)'/" file1

Awk approach:

awk '$1=="keyword:"{ getline k < "file2"; print $1, "\047" k "\047"; next }1' file1

The output:

not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something'

Awk approach:

awk '$1=="keyword:"{ getline k < "file2"; print $1, "\047" k "\047"; next }1' file1

The output:

not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something'

As an alternative use sed in-place substitution:

$ sed -Ei "s/^(keyword: ).*/\1'$(cat file2)'/" file1
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Awk approach:

awk '$1=="keyword:"{ getline k < "file2"; print $1, "\047" k "\047"; next }1' file1

The output:

not_keyword: 'something'
keyword: 'condition 1 condition 2 condition 3'
another_not_keyword: 'something'