Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern matching lines using the date, and then joining the lines

Status
Not open for further replies.

HIB049

Technical User
Jan 31, 2003
19
GB
Hi Guys,

Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated.

Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n"

TIME SERVER/CLIENT TEXT
02/03/2010 23:44:13 hostname02 hostname01 NBU status: 96, EMM status: No media is
available
01/03/2010 03:48:14 unknown hostname01 backup of client hostname01 exited with
status 96 (unable to allocate new media for backup,
storage unit has none available)
22/02/2010 19:44:09 hostname02 hostname01 NBU status: 96, EMM status: No media is
available

OUTPUT FILE:
TIME SERVER/CLIENT TEXT
02/03/2010 23:44:13 hostname02 hostname01 NBU status: 96, EMM status: No media is available
01/03/2010 03:48:14 unknown hostname01 backup of client hostname01 exited with status 96 (unable to allocate new media for backup, storage unit has none available)
22/02/2010 19:44:09 hostname02 hostname01 NBU status: 96, EMM status: No media is available
 
tried using awk '/[0-9]*\/[0-9]*\/[0-9]*/ { print $0; getline; print $0}', but this doesnt work.
 
Hi

You were quite close. Just need to :
[ul]
[li]Stop [tt]awk[/tt] to output the line separator at the end of non-final lines ( use [tt]printf[/tt] )[/li]
[li]Handle multiple trailing lines ( use [tt]while[/tt] )[/li]
[/ul]
But in my approach there would be a little change :
[ul]
[li]Set the [tt]ORS[/tt] ( Output Record Separator ) to nothing[/li]
[li]Output [tt]\n[/tt] ( new line ) when needed[/li]
[/ul]
Code:
awk -vORS= '/[0-9]+\/[0-9]+\/[0-9]+/{print"\n"}{sub(/^ +/," ")}1' /input/file
Note that with this approach there will be no newline at the end of the last line. If you need that too, just [tt]print[/tt] it in an [tt]END[/tt] block.

Feherke.
 
Thks feherke, that worked beautifully.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top