Reformatting single column text file starting new line when finding particular string

Hi, I have a single colum file and I need to reformat the file so that it creates a new line every time it come to an IP address and the following lines are corresponding rows until it comes to the next IP address.

I want to turn this

172.xx.xx.xx
 gwpusprdrp02_pv
 seinwnprd03
10.xx.xx.xx
 imauditorsql
 seinwnprd06
172.xx.xx.xx
 invtsql01
 seinwnprd02
172.xx.xx.xx
 itcnetlog1
 seinwnprd02
172.xx.xx.xx
 itcsql04
 seinwnprd02
10.xx.xx.xx
 oaksdag01.corp.seic.com
 seinwnprd06, nsrserverhost
 

into this

172.xx.xx.xx gwpusprdrp02_pv seinwnprd03
10.xx.xx.xx imauditorsql seinwnprd06
172.xx.xx.xx invtsql01 seinwnprd02
172.xx.xx.xx itcnetlog1 seinwnprd02
172.xx.xx.xx itcsql04 seinwnprd02
10.xx.xx.xx oaksdag01.corp.seic.com seinwnprd06, nsrserverhost
nawk 'ORS=(FNR%3)?FS:RS' myFile
1 Like

Thanks for the response but the thing is that 3rd line does not always start with the IP address. I am looking for an awk statement that search for a string that starts with 1 so '^1' and then turns the lines that follow into corresponding rows for the IP address until it comes to the next IP?

nawk '{printf("%c%s", ((/^[0-9]+/&&FNR!=1)?ORS:""), $0)}END{print ""}' myFile

Thanks again but that code is only printing the IP addresses.

given myFile:

172.xx.xx.xx
 gwpusprdrp02_pv
 seinwnprd03
10.xx.xx.xx
 imauditorsql
 seinwnprd06
172.xx.xx.xx
 invtsql01
 seinwnprd02
172.xx.xx.xx
 itcnetlog1
 seinwnprd02
172.xx.xx.xx
 itcsql04
 seinwnprd02
10.xx.xx.xx
 oaksdag01.corp.seic.com
 seinwnprd06, nsrserverhost

running nawk '{printf("%c%s", ((/^[0-9]+/&&FNR!=1)?ORS:""), $0)}END{print ""}' myFile produces:

172.xx.xx.xx gwpusprdrp02_pv seinwnprd03
10.xx.xx.xx imauditorsql seinwnprd06
172.xx.xx.xx invtsql01 seinwnprd02
172.xx.xx.xx itcnetlog1 seinwnprd02
172.xx.xx.xx itcsql04 seinwnprd02
10.xx.xx.xx oaksdag01.corp.seic.com seinwnprd06, nsrserverhost

Check your myFile input file. Make sure it doesn't contain any ^M-s. Post the output of:

cat -vet myFile

It works perfectly if I use /usr/xpg4/bin/awk instead of /usr/bin/nawk. Thanks so much!!

very strange.........