1

I have a file which is as follows:

------------------------------------------------------------------------------------------------------------------------------------------------------------------
49515 23/6/2014 SL B                                              .OO                                  2500.00
          R ROY                             4561235
BEING THE T.E PAID
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
23495 26/7/2014  CL A                                     2300.00                                         .00
          S DAS                             2334167
BEING THE MONEY RECOVERED 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

My desired output is as follows:

49515 23/6/2014  SL B                      .00                2500.00       R ROY          4561235        BEING THE T.E PAID
23495  26/7/2014  CL A             2300.00                        .00       S DAS          2334167       BEING THE MONEY RECOVERED

I tried the following code in awk without much success, the idea being to set newline as field separator and the line of hyphens (--------) as the record separator:

BEGIN {
   FS="/n"
   RS="^-+$"}
{ print $1,$2,$3}
2
  • The FS=/n" is just a typo, right? You were using FS="\n"?
    – terdon
    Dec 10 '15 at 14:47
  • Did the answers solved your problem? We can't know for sure until one answer gets accepted.
    – Kira
    Dec 11 '15 at 19:59
3

This should work for your needs:

awk '/---/ {print buff; buff="";} /[^-]/{buff=buff" "$0}'  filename

You can use if-else blocks too:

awk '{if($0 ~ /---/){print buff; buff=""} else {buff=buff" "$0}}' filename

Also without using a buffer:

awk '{if($0 !~ /---/){printf "%s ", $0} else print ""}' filename
2
  • Nice. You might want to only print unless NR==1 though to avoid printing a blank line at the beginning.
    – terdon
    Dec 10 '15 at 14:43
  • True, but I thought a shorter code would be better (and easier to understand) than the one adding this extra checking just because of an extra blank line. Thanks for the comment though.
    – Kira
    Dec 10 '15 at 14:55
1

You were almost there:

$ awk -F'\n' -vRS="-+\n" '($1){print $1,$2,$3; }' file
49515 23/6/2014 SL B                                              .OO                                  2500.00           R ROY                             4561235 BEING THE T.E PAID
23495 26/7/2014  CL A                                     2300.00                                         .00           S DAS                             2334167 BEING THE MONEY RECOVERED 

Or, if you prefer the BEGIN block:

awk 'BEGIN{FS="\n"; RS="-+\n"}($1){print $1,$2,$3; }' file

The problem (assuming the /n was a typo) was that you were using ^ and $ in the definition of RS. I'm not sure about the internals of how the RS regex is implemented but I would guess that they actually refer to the beginning and end of the file, not the line. As a workaround, I set RS to a stretch of - ending in a newline. However, this means that it will break if you can have one or more - at the end of a line. I don't know how to match from the beginning since \n-+\n fails for the first line.

A similar approach would be to replace ^-+$ with a blank line and use perl's paragraph mode:

$ sed 's/--*/\n/' file | perl -F'\n' -00ane 'print "@F\n";' 
49515 23/6/2014 SL B                                              .OO                                  2500.00           R ROY                             4561235 BEING THE T.E PAID
23495 26/7/2014  CL A                                     2300.00                                         .00           S DAS                             2334167 BEING THE MONEY RECOVERED 

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.