Home > Back-end >  copy a part of a line from file1 to specific place in file2 using sed or any other command
copy a part of a line from file1 to specific place in file2 using sed or any other command

Time:12-02

hi friends im new to linux terminal and its commands and i need help to solve this situation:

i have 2 files :

cat file1:
line1
line2
key=
line4


cat file2:
line1
line2
line3
secret: 12345
line5

what i need to do is to copy "12345" from file2 and paste it infront of "key=" in file1 and the important thing is that the secret value (in this example 12345) changes every time and for another server its not 12345 anymore.how can i do this with sed or anything else? thanks for your help

CodePudding user response:

The next command should work on Ubuntu:

cat file2 | grep "secret:" | tail -1 | sed 's/^secret://' | xargs -I{} sed  -i 's/key=.*$/key={}/' file1

The next command works on my MacOS:

cat file2 | grep "secret:" | tail -1 | sed 's/^secret://' | xargs -I{} sed  -i.backup 's/key=.*$/key={}/' file1
  • Related