If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > join previous & current line based on match

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-10-05, 08:47
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Question join previous & current line based on match

can anyone please suggest how to do this? i believe perl or awk may
have easy solution.


what i am trying to achieve is to search for error code, if found then write Joining previous line and current line



here is input file layout

Record 1:xxxxxxxx
Error A:YYYYYYYY

Record 2:xxxxxxxx
Error A:YYYYYYYY

Record 3:xxxxxxxx
Error B:YYYYYYYY

Record 4:xxxxxxxx
Error A:YYYYYYYY

Record 5:xxxxxxxx
Error C:YYYYYYYY


=============================
desired output

Record 1:xxxxxxxx::Error A:YYYYYYYY
Record 2:xxxxxxxx::Error A:YYYYYYYY
Record 3:xxxxxxxx::Error B:YYYYYYYY
Record 4:xxxxxxxx::Error A:YYYYYYYY
Record 5:xxxxxxxx::Error C:YYYYYYYY

Thanks,
Reply With Quote
  #2 (permalink)  
Old 10-10-05, 11:49
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
sdk,
try this:

awk '{if(substr($0,1,5)=="ERROR") print prev, $0;prev=$0}' my.file.txt

bye,
ducasio
Reply With Quote
  #3 (permalink)  
Old 10-10-05, 11:59
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Smile

Thanks ducasio,

was not aware that like next, there is prev also

Thanks for quick help.
Reply With Quote
  #4 (permalink)  
Old 10-11-05, 04:08
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
prev is just a variable I declare and use. I could name it i as well and it would be writen like this:

awk '{if(substr($0,1,5)=="ERROR") print i, $0;i=$0}' my.file.txt

bye,
ducasio
Reply With Quote
  #5 (permalink)  
Old 10-11-05, 08:11
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Later I realized same.Thanks again for clarification.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On