1

I'd like to separate record by first word (eg DEBUG or INFO) and keep RS.

but execute program, awk removes RS.

How to keep it?

log.txt is

DEBUG:[2018-04-09 13:00:01]
=========================
START LOG 
:
:
END LOG 
===========================

DEBUG:[2018-04-09 13:00:02]
 INFO:[2018-04-09 13:00:03]
DEBUG:[2018-04-09 13:00:04]
=========================
START LOG 
:
:
END LOG 
===========================

my trying program is

$gawk 'BEGIN{RS="(DEBUG|INFO)"; FS="\n"}{print RS$0}' log.txt

but it shows

(DEBUG|INFO):[2018-04-09 13:00:01]
=========================
START LOG 
:
:
END LOG 
===========================

(DEBUG|INFO):[2018-04-09 13:00:02]
(DEBUG|INFO):[2018-04-09 13:00:03]
(DEBUG|INFO):[2018-04-09 13:00:04]
=========================
START LOG 
:
:
END LOG 
===========================
  • What output you expect? – αғsнιη Apr 12 '18 at 7:36
3

You can use RT

 gawk 'BEGIN{RS="(DEBUG|INFO)"; FS="\n"}{printf "%s%s", $0, RT}' log.txt 
  • 1
    Worth pointing out that this is a GNU extension to awk not available in BSD awk nor in mawk. – Kusalananda Apr 12 '18 at 7:28

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.