Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
If have a sql-dump, wherein records can contain a <LF>. I want to join these "multiple line" records into 1 line.
The file contains data like:
B1CO38EHHRA0Z3|me_draw_tekam | 917356137|......|...|
B1CO3999999993|me_draw_tekam
special| 917356137|......|...|Each line must begin with 14 char. followed by a "|". If the 15-char. isn't a "|", the line must be joined with the previous one (I know this isn't always correct).
I've tried:
sed 's/\n\(..............[^|].*\)/<CRLF>\1/'and
:join
/.*\n..............[^|].*/{N
s/\n\(..............[^|].*\)/<CRLF>\1/
b join
}But both methods does not work.
Any suggestions?

My first suggestion is to try to avoid the wrapped lines to begin with. In Oracle sqlplus, increase the width of an output line so that one entire logical line will fit on one physical line:
set linesize 250

It's not Oracle, but Allbase (an old database from HP). I've set the width of each record, but some fields can contain a <LF>. These fields were used by users to enter a multi-line note.
I don't know, within Allbase, how to avoid the output of these <LF>, so i'm trying to do this afterwards.

Here is an awk solution. That match expression insists on 14 non-bar characters followed by a bar character followed by anything. It would recognized the first line below as a "starting" line, but not the second:
aaaaaaaaaaaaaa|cccc|
aaaaaa|bbbbbbb|cccc|awk '{
if (match($0,"^[^|]{14,14}\|"))
{if ( out!="" ) print out
out=$0}
else
out=out $0
}
END {if ( out!="" ) print out}' file.inA sed solution will be a bit more challenging (for me at least). But I will give it a try.

Don't we love awk!
I started with awk, but gave it up. I see now that I took the wrong approach.
Thanks for your input, so no need for sed anymore.

For practice, I coded a sed solution for this. The rules that I followed are: Join all lines together, but a line beginning with NEW will start a new line. The first and last lines of the file may or may not be a NEW line. The file may have only one line.
sed -e '1{$p;x;d;}' \
-e '/^NEW/!{H;$!d;x;s/\n//g;b;}' \
-e 'x;s/\n//g;${p;x;}' \
fileinWhile the above does work for me on a non-HPUX box, my HPUX box does not allow me to have a b within braces (what a pain). In this particular case however, I am able to make a simple change that will work on HPUX:
sed -e '1{$p;x;d;}' \
-e '/^NEW/!{H;$!d;x;s/\n//g;p;d;}' \
-e 'x;s/\n//g;${p;x;}' \
fileinOr, here is a slight variation that will work on HPUX or not. The branch to the :s label avoids the need for that particular set of braces.
sed -e '1{$p;x;d;}' \
-e '/^NEW/bs' \
-e 'H;$!d;x;s/\n//g;b' \
-e :s \
-e 'x;s/\n//g;${p;x;}' \
fileinTo adapt this code to the original problem, the starter lines, rather than being identified with /^NEW/, are identifed as starting with 14 non bar characters followed by a bar character: /^[^|]\{14,14\}|/

You're quite welcome.
That sed script was a good exercise for my practice, and would be a good intermediate script to study, but I would not suggest trying to start with it.
You will find "HANDY ONE-LINERS FOR SED" at:
http://www.student.northpark.edu/pemente/sed/sed1line.txtThose are great for self study (along with the man page, of course).

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |