6

I have a file like this:

< Dec 2, 2015 2:51:49 PM EST> <Error> <HTTP> <cphypprod1v..com> 
<AnalyticProviderServices0> <[ACTIVE] ExecuteThread: '3' for queue: 
'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel> <> <1449085909249> <BEA-

101017> <[ServletContext@462961596[app:bea_wls_deployment_internal 
module:bea_wls_deployment_internal.war path:/bea_wls_deployment_internal spec-

version:null]] Root  ServletException.

java.lang.OutOfMemoryError: GC overhead limit exceeded 

>

< Dec 2, 2015 2:51:49 PM EST> <Warning> <RMI> <cphypprod1v.sherwin.com>   <AnalyticProviderServices0> <[STANDBY] ExecuteThread: '8' for queue:   'weblogic.kernel.Default (self-tuning)'>  
<<WLS Kernel>> <> <> <1449085909264> < BEA-080003> < RuntimeException thrown by  
rmi server: javax.management.remote.rmi.RMIConnectionImpl.invoke 
(Ljavax.management.ObjectName;Ljava.lang.String;Ljava.rmi.Marshal 

>

I need to modify it so it looks like:

  < Dec 2, 2015 2:51:49 PM EST> <Error> <HTTP> <cphypprod1v..com> <AnalyticProviderServices0> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel> <><1449085909249> <BEA-101017> <[ServletContext@462961596[app:bea_wls_deployment_internal module:bea_wls_deployment_internal.war path:/bea_wls_deployment_internal spec-version:null]] Root  ServletException. java.lang.OutOfMemoryError: GC overhead limit exceeded  >

< Dec 2, 2015 2:51:49 PM EST> <Warning> <RMI> <cphypprod1v.sherwin.com>   <AnalyticProviderServices0> <[STANDBY] ExecuteThread: '8' for queue:   'weblogic.kernel.Default (self-tuning)'>  
<<WLS Kernel>> <> <> < 1449085909264> < BEA-080003> < RuntimeException thrown by  rmi server: javax.management.remote.rmi.RMIConnectionImpl.invoke (Ljavax.management.ObjectName;Ljava.lang.String;Ljava.rmi.Marshal >

Everything between < and > must be on the same line. How can I do this?

  • Hi and welcome to the site. Please use the formatting tools to format your example files as code. I have edited your question to what I think you meant, but please make sure I didn't make any mistakes. – terdon Jul 27 '16 at 18:39
  • Is the <br> actually part of your file? – terdon Jul 27 '16 at 18:40
  • no..i changed it. – Naresh Jul 27 '16 at 18:46
5

With awk

awk 'BEGIN{RS=">\n+";ORS=">\n";FS="\n"} {$1=$1}1' yourfile
< Jan 20, 2016 11:58:09 AM EST  Test1 Sample Test1 >
< Jan 20, 2016 11:58:09 AM EST Sample Test It is not  T1 T2 >

If you want a blank line between each output, you can add an extra \n to the ORS i.e.

awk 'BEGIN{RS=">\n+";ORS=">\n\n";FS="\n"} {$1=$1}1' yourfile

(although this may also add a trailing blank line at the end of the file).

  • If i want space between each line.. how to do in that case – Naresh Jul 27 '16 at 20:03
  • @Naresh please see updated answer - note that @StephaneChazelas nice sed answer does not suffer from the extra trailing newline – steeldriver Jul 27 '16 at 20:16
  • but i am getting all the lines in one line. – Naresh Jul 27 '16 at 20:31
  • i have file in the same format , when i use this command i am getting everything in one line... – Naresh Jul 27 '16 at 20:48
  • I have change the format as per my file .. please help me on this – Naresh Jul 27 '16 at 20:57
3

Here you are:

For (GNU sed):

sed -e ':x' -e 'N' -e '$!bx' -e 's/\n/ /g' -e 's/ </\n</g' yourFile

For (BSD sed):

sed -e ':x' -e 'N' -e '$!bx' -e 's/\n/ /g' -e 's/ \</\'$'\n</g' yourFile 

This is how I've done it:

  • Create a label via :x
  • Append the lines to the pattern space with N
  • Branch to the created label - the x at the first of the command - $!bx so it won't apply the space-associated substitution on the last line since we should keep the last newline
  • Then the substitution replaces every newline with a space() on the pattern space
  • And then another substitution replaces every < followed by a space with a newline followed by a <.
  • 1
    In GNU sed, the < in the final expression needs to be UNescaped I think i.e. 's/ </\'$'\n</g' or just 's/ </\n</g' (else it's treated as a word boundary rather than a literal <) – steeldriver Jul 27 '16 at 19:33
  • @steeldriver yes you're utterly right, thanks mate. I'm updating my answer. – FarazX Jul 27 '16 at 19:47
3

Looks like in effect, you want to remove all the newline characters except those that follow a >, so:

perl -pe 's/(?<!>)\n//'

would do. (?<!...) is a negative look behind operator. So, it's \n provided it's not preceded with a >.

If it's to remove all newline characters that are between matching <...> pairs and, as per your new sample, those may nest, then that becomes more complicated:

perl -0777 -pe 's{<(?:(?0)|[^<>])*>}{$& =~ s/\n//gr}gse'

Here using recursion in perl regexps ((?0) refers to the whole regexp again).

1

Using awk, paste and sed

awk '/</{a=1}/>/{print;a=0}a' file | paste -d "" -s | sed 's/></>\n</g'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.