1

Want to change everything after security.server.ip=* with the result ip from the second grep.

First Grep:

cat admin.conf|grep security.server.ip|grep -v ^#

Result:

security.server.ip=10.10.1.2

Second Grep:

cat /etc/hosts|grep -i admin-server|head -1|awk '{ print $1}

Result:

10.10.1.2

Sometimes security.server.ip will be different on admin.conf and I'm wondering how to replace it with one command which will catch IP address form second grep and replace it in the first one.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23
  • possible duplicate of [sed: Replace part of a line](http://stackoverflow.com/questions/1074450/sed-replace-part-of-a-line). Just store the values in a variable and then use answers in that question. – fedorqui Aug 05 '14 at 13:34
  • Just use awk for all of it, the cat at the start is also completely pointless as the commands take files as arguments –  Aug 05 '14 at 13:58
  • I'm not able to figure it out how to do it, even the below examples not work! – Kalin Borisov Aug 05 '14 at 14:00
  • Write your question more coherently and you will get better answers, give expected output and try to explain what you actually want better as it is not very clear. –  Aug 05 '14 at 14:02

3 Answers3

1

You can use a script:

#!/bin/sh
IP=$(exec grep -i admin-server /etc/hosts | awk '{ print $1; exit }')
sed -i "/^security\.server\.ip=/s|=.*|=$IP|" admin.conf
konsolebox
  • 72,135
  • 12
  • 99
  • 105
1

You could save it in a variable:

NEWIP=`grep -i admin-server /etc/hosts|head -1|awk '{ print $1}'` \
sed -i "s/^security\.server\.ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/security\.server\.ip=$NEWIP/" admin.conf
martin
  • 3,149
  • 1
  • 24
  • 35
0

With GNU awk for inplace editing, nextfile, and gensub():

gawk -i inplace '
    NR==FNR{ if (tolower($0) ~ /admin-server/) { ip=$1; nextfile } next }
    { $0=gensub(/(security\.server\.ip=).*/,"\\1"ip,""); print }
' /etc/hosts admin.conf
Ed Morton
  • 188,023
  • 17
  • 78
  • 185