The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
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"..

Code:
sed 's/\
/#/g' f2
My source data looks like below which has 3 lines, all the line starts with "PROD~".

Code:
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

Quote:
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

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

Code:
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-04-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 964
The following should do what you want.

Code:
sed -n -e 'N;s/\n/#/;P;D;' sourcefile
Reply With Quote
  #3 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Thanks a ton !!

May Í know the reason why 'D' is used in the command?
Reply With Quote
  #4 (permalink)  
Old 02-04-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
ah, it's an RTFM(Read The Manual First) problem

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 ?
Reply With Quote
  #5 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
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

Last edited by brainyoung; 02-05-2008 at 02:30 AM.
Reply With Quote
  #6 (permalink)  
Old 02-05-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,996
If awk is allowed:

Code:
awk 'BEGIN{ORS=""} NR>1{gsub(/PROD~N/,"\nPROD~N")}{print} END{printf("\n")}' file
Regards
Reply With Quote
  #7 (permalink)  
Old 02-06-2008
Registered User
 

Join Date: Jan 2008
Posts: 13
Thanks for the idea Franklin !!, I shall enhance this to suite my requirment
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0