move the last word to begining of next line - SED

Hello,

I'm trying to move the last word of matching pattern to the begining of next line. Appreciate if anyone post the script.

From the below line I'm getting the last word, Note: this word also appears in many places in my file

#return the last word of line that contains ListenPort
sed -n -e 's/.*\(ListenPort="[0-9][0-9][0-9][0-9][0-9]"\)$/\1/p' test.txt

Input:

ListenAddress="xyz.com" ListenPort="11111"
Name="V1Server" NativeIOEnabled="true"
<Machine Name="xyz">
   <Manager ListenAddress="" ListenPort="11113" Name="xyz"/>
</Machine>

ListenAddress="xyz.com" ListenPort="11112"
Machine="xyz" Name="V2Server"

Expected Output:

ListenAddress="xyz.com" 
ListenPort="11111" Name="V1Server" NativeIOEnabled="true"
<Machine Name="xyz">
   <Manager ListenAddress="" ListenPort="11113" Name="xyz"/>
</Machine>
ListenAddress="xyz.com" 
ListenPort="11112" Machine="xyz" Name="V2Server"

Thanks

This should give the expected ouput:

awk '/ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' input

Regards

Thanks for your reply

I still could not make it work due to syntax. Again, the pattern ListenAddress also appears in the middle of line which I dont want to tocuch it.

awk '/ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' File1
awk: syntax error near line 1
awk: bailing out near line 1

Thanks again

use nawk or /usr/xpg4/bin/awk for this syntax...

Sorry, to search for the pattern only on the beginning of the line:

awk '/^ListenAddress=/{printf("%s\n%s ", $1, $2);next}$0{print}' input

If you get errors, use gwk, nawk or /usr/xpg4/bin/awk on Solaris

Regards