Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2003
    Posts
    71

    Question Unanswered: 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,

  2. #2
    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

  3. #3
    Join Date
    Sep 2003
    Posts
    71

    Smile

    Thanks ducasio,

    was not aware that like next, there is prev also

    Thanks for quick help.

  4. #4
    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

  5. #5
    Join Date
    Sep 2003
    Posts
    71
    Later I realized same.Thanks again for clarification.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •