SED question

Hello there,

I've seen quite a few post on SED to handle newline, but I tried few things doesn't seem to work.

I was able to replace any tex tto newline, however viceversa (new line on Solaris returns error message as 'SED garbled"..

sed 's/\
/#/g' f2

My source data looks like below which has 3 lines, all the line starts with "PROD~".

PROD~N_PROD~kf_XXX_IHIRS_TK_REP~kf_XXX_IHIRS_TK_REP~TL:- changend decimalseparator from '.' to ','
- expanded the source-sql to all the iHIRS-Indices.~UPD
PROD~N_PROD~X_GEN_PX_TKD_REP~X_GEN_PX_TKD_REP~TL:
- remove the sort-option from the source-sql and inserted a sort-transaction into the mapping~UPD
PROD~N_PROD~kf_X_GEN_PX_TKD_REP~kf_X_GEN_PX_TKD_REP~TL:- changend the source-sql because of 
performance issues~UPD

Ideally my regular expression should do the following.

Step 1 : convert \n to # or something else, so that I have one line as below

Step 2: convert #PROD~ to #PROD~\n, so the output is as below

PROD~N_PROD~kf_XXX_IHIRS_TK_REP~kf_XXX_IHIRS_TK_REP~TL:- changend decimalseparator from '.' to ','#- expanded the source-sql to all the iHIRS-Indices.~UPD
PROD~N_PROD~X_GEN_PX_TKD_REP~X_GEN_PX_TKD_REP~TL:#- remove the sort-option from the source-sql and inserted a sort-transaction into the mapping~UPD
PROD~N_PROD~kf_X_GEN_PX_TKD_REP~kf_X_GEN_PX_TKD_REP~TL:- changend the source-sql because of #performance issues~UPD

since I'm unable to convert the newline(\n) to something, I couldn't really move forward. I appreciate any pointers.

Thanks

The following should do what you want.

sed -n -e 'N;s/\n/#/;P;D;' sourcefile

Thanks a ton !!

May � know the reason why 'D' is used in the command?

ah, it's an RTFM(Read The Manual First) problem :slight_smile:

Using D the lines already read being deleted from the buffer. Thanks anyway

Since 'N' is going to append only the next line,so if a line spans for more than 2 rows, will the result be similar ?

Hi fmurphy,

If a record spans to more than 2 lines then above isn't working. output is kind of garbled.I've managed to handle in my application. However, I'm curious to know if it's also feasible with sed/shell

Thanks

If awk is allowed:

awk 'BEGIN{ORS=""} NR>1{gsub(/PROD~N/,"\nPROD~N")}{print} END{printf("\n")}' file

Regards

Thanks for the idea Franklin !!, I shall enhance this to suite my requirment