I have a file having the below values:
cat data.txt
server1: calv
server2: anot
log: /u/log/1
server3: calv
server4: anot
server5: secd
server6: calv
LIB_TRGT_calv,anot: /tmp/hello.txt
LIB_TRGT_secd: /var/del.tmp
I get the variables starting with LIB_TRGT
i.e LIB_TRGT_calv,anot
& LIB_TRGT_secd
I need to get the names after TRGT_
from the variable above i.e calv,anot.
considering we got calv & anot
; I need to get all the entries having calv
& anot
and add the entries found in the data.txt as below:
Desired Output:
server1: calv
server2: anot
log: /u/log/1
server3: calv
server4: anot
server5: secd
server6: calv
LIB_TRGT_calv,anot: /tmp/hello.txt
LIB_TRGT_secd: /var/del.tmp
LIB_server1: /tmp/hello.txt
LIB_server2: /tmp/hello.txt
LIB_server3: /tmp/hello.txt
LIB_server4: /tmp/hello.txt
LIB_server6: /tmp/hello.txt
LIB_server5: /var/del.tmp
Likewise for LIB_TRGT_secd
Below is what I did so far:
grep TRGT* data.txt | cut -d: -f1 |cut -d_ -f3
calv,anot
secd
further
grep TRGT* test.txt | cut -d: -f1 |cut -d_ -f3 | sed -n 1'p' | tr ',' '\n'
calv
anot
However, secd
is missing and i m not sure how to use xargs
and further go about it.
Tried the solution by user @Kusalananda but it does not work. See output below:
LIB_TRGT_calv,anot: /tmp/hello.txt
and not something likeLIB_TRGT_calv,LIB_TRGT_anot: /tmp/hello.txt
.?LIB_TRGT_calv,anot: /tmp/hello.txt