Search code examples
linuxbashawksedcentos7

How can I add a 2 lines form one variable to a specific line from another variable?


I have these variables which outputs:

$nw_monitor
Host has 'em3, em2, em1, em4' network devices
em3 does not have any ip rules, skipping health checks
Checking health of 'em2'
Checking health of 'em1'
em4 does not have any ip rules, skipping health checks
Host has 2 healthy network links

And the second variable.

$link
Speed              : 10 Gb/s
Link status        : yes
Speed              : 10 Gb/s
Link status        : yes

From the second variable, it gives the speed and link status for each interface mentioned in $nw_monitor, em2 and em1.

So I would like to mix them so it looks like this:

Host has 'em3, em2, em1, em4' network devices
em3 does not have any ip rules, skipping health checks
Checking health of 'em2'
Speed              : 10 Gb/s
Link status        : yes
Checking health of 'em1'
Speed              : 10 Gb/s
Link status        : yes
em4 does not have any ip rules, skipping health checks
Host has 2 healthy network links

I have tried this:

sed /^Checking/R<(echo "$link") <(echo "$nw_monitor")

BUT, I get his output:

Host has 'em3, em2, em1, em4' network devices
em3 does not have any ip rules, skipping health checks
Checking health of 'em2'
Speed              : 10 Gb/s
Checking health of 'em1'
Link detected      : yes
em4 does not have any ip rules, skipping health checks
Host has 2 healthy network links

Solution

  • Feed in each in separate data streams.
    You can spawn numbered streams for this sort of thing.

    $: exec 3< <(seq 1 3) # stream 3 is now numbers
    $: cat <&3
    1
    2
    3
    $: printf "%s\n" a b c>foo
    $: for x in . . . .; do read y <&3; echo "[$y]"; done 3<foo
    [a]
    [b]
    [c]
    []
    

    Why are they in strings? I think this would be easier in simple files.

    $: echo "$nw_monitor">a
    $: echo "$link">b
    $: while read a
    >  do echo "$a"
    >     case "$a" in Checking*) for c in 1 2; do read b <&3; echo " $b"; done;; esac
    >  done <a 3<b
    Host has 'em3, em2, em1, em4' network devices
    em3 does not have any ip rules, skipping health checks
    Checking health of 'em2'
     Speed              : 10 Gb/s
     Link status        : yes
    Checking health of 'em1'
     Speed              : 10 Gb/s
     Link status        : yes
    em4 does not have any ip rules, skipping health checks
    Host has 2 healthy network links
    

    But if you just need them in strings...

    $: while read a
    >  do echo "$a"
    >     case "$a" in Checking*) for c in 1 2; do read b <&3; echo " $b"; done;; esac
    >  done < <(echo "$nw_monitor") 3< <(echo "$link")
    Host has 'em3, em2, em1, em4' network devices
    em3 does not have any ip rules, skipping health checks
    Checking health of 'em2'
     Speed              : 10 Gb/s
     Link status        : yes
    Checking health of 'em1'
     Speed              : 10 Gb/s
     Link status        : yes
    em4 does not have any ip rules, skipping health checks
    Host has 2 healthy network links