1

I wanted to extract the IPs under [abc] from file1, and use them to replace the list of hosts in file 2:

File1:

[abc]
192.168.29.153
192.168.29.155
[def]
192.168.29.153
[xyz]
192.168.29.153

File 2:

output.logstash:
  # The Logstash hosts
  hosts: ["192.168.29.115:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"
  • what goes where? I don't see any connection between "abc" or "192.168.29.153" or "192.168.29.115" ... – Jeff Schaller Feb 5 '18 at 19:19
  • I want to extract the lines below [abc] : 192.168.29.153 192.168.29.155 and put them in the second file on the list of hosts just like that: hosts: ["192.168.29.153","192.168.29.155"] – seyfs Feb 5 '18 at 19:21
  • Why does the .115 host have a port with it? Do they all need that port added? – Jeff Schaller Feb 5 '18 at 19:49
  • yes, but I can add it manually on the file. No problem with that – seyfs Feb 5 '18 at 19:51
0

I think this does it, if awk is ok.

#! /usr/bin/awk -f

# here, NR is record number, and FNR is *file* record number - this matches only 
# in first file
NR == FNR {
    # if first field matches
    if ($1 ~ /abc/) {
        # arrange addresses with `", "` between them
        abc=$2
        for (i=3; i<=NF; i++) abc = abc "\", \"" $i
    } 
    # then we're finished with this file.
    next;
}

$1 ~ /hosts/ {
    # print the saved addresses formatted with `["` addr `"]`
    printf "  %s [\"%s\"]\n", $1, abc ;
    next
 }

# print any other lines with no editing.
{ print }

Gives output for the updated file as the below. Note that the built in variable RS (record seperator) is being updated between files though, to treat each separately.

~$ ./log_edit RS='[' hosts RS='\n' logstash
output.logstash:
  # The Logstash hosts
   hosts: ["192.168.29.153", "192.168.29.155"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem

You'll also need to save the output then replace the old file.

  • oh, I didn't see that there had been a change to the question.. – Guy Feb 5 '18 at 19:28
  • sorry yes.. because it a list of IPs not only one – seyfs Feb 5 '18 at 19:32
  • I tried to run the awk command like that : awk script_awk fileabc filehosts but nothing was changed on the filehosts ... still hosts: ["192.168.29.115:5044"] – seyfs Feb 5 '18 at 20:04
  • You need to call it like it is in the example, including the RS='...' file1 RS='...' file2. Otherwise it's not using the correct idea of a record. THe first file treats between [ characters like a line, almost. – Guy Feb 5 '18 at 20:07
  • Thank you so much ! but is it possible to put the new hosts line on the file logstash instead of the old line ? I mean apply the changes to the file itself and not to just display it on the output. Thx for your help ! – seyfs Feb 5 '18 at 20:25

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.