Welcome to LQ!
Please place your code snippets inside [CODE]...[/CODE]
tags for better readability. You may type those yourself or click the "
#" button in the edit controls.
I find your description a bit confusing.
Do you expect a single result from this?
Code:
grep serverA /usr/local/hosts/*/*
=> HT_dev/SUNOS:serverA
If so, and you want only the first part of the path then you may set it into a variable, like this...
Code:
PREFIX=$(grep serverA /usr/local/hosts/*/*)
...and refer to the first part like this...
${PREFIX%%/*}
And if you are making the change to a single file, your second file, using the same grep term, a simple sed command should do it...
Code:
sed -n s/^serverA,/${PREFIX%%/*},\&/p secondfile
So, given a second file as shown, and the same grep results...
Code:
# cat secondfile
serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
serverB,appA 3.1.5,appB 5.2.0,appC 6.2.0
serverXYZ123,appA 3.2.0,appB 5.0.0,appC 6.2.0
# echo $PREFIX
HT_dev/SUNOS:serverA
# sed -n s/^serverA,/${PREFIX%%/*},\&/p secondfile
HT_dev,serverA,appA 3.2.0,appB 5.0.0,appC 6.2.0
Adapt that to your own case and let us know if it works for you. The example here will not modify the second file, it will only print out the changed lines to your terminal.
Hope this helps!