To remove new line character | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

To remove new line character

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-10-2005
shihabvk shihabvk is offline
Registered User
 
Join Date: May 2005
Last Activity: 14 July 2009, 8:56 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
To remove new line character

Hi,
I am facing one interesting problem :
I have a file which contains data like this
459,|1998-11-047|a |b |c \n efg | d|e | \n
459,|1998-11-047|a \n c|b |c \n efg | d|e | \n
Basically what I have to do is , I have to remove all \n which is coming ( enclosed ) in between two pipes ( | ).. The red coloured only...
The problem I am facing is , when i try to do it using sed .. because of this \n , sed takes the entries after it as next record.

Could you please help me

Shihab
Sponsored Links
    #2  
Old 08-10-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 31 October 2014, 6:07 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
sed -e 's#|\([^|][^|]*\)\\n\([^|][^|]*\)|#|\1\2|#g' file

Sponsored Links
    #3  
Old 08-12-2005
shihabvk shihabvk is offline
Registered User
 
Join Date: May 2005
Last Activity: 14 July 2009, 8:56 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your help vgersh99

But it is not working
I will explain my problem with an example

Here is the input file
$cat input.dat
|a|b
c|d|
|e|f|i|
|h|i|j|

and out put should be like this
$cat output.dat
|a|bc|d|
|e|f|i|
|h|i|j|

Here the first two lines have been joined since new line is not in between |

Thanks in advance
Shihab
    #4  
Old 08-12-2005
Unbeliever Unbeliever is offline
Registered User
 
Join Date: Jul 2005
Last Activity: 14 September 2007, 11:54 AM EDT
Location: England
Posts: 183
Thanks: 0
Thanked 1 Time in 1 Post
perl -e 'while (<>) { if (! /\|$/ ) { chomp; } print ;}' input.dat > output.dat

should work assuming the correct end of line is always preceeded with a '|' character.

If you have trailing spaces or suchlike on the lines then it needs a little modification.
Sponsored Links
    #5  
Old 08-12-2005
jerardfjay jerardfjay is offline
Registered User
 
Join Date: Feb 2005
Last Activity: 9 June 2010, 12:46 PM EDT
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
use tr


Code:
tr -d '\n\' < file.dat

where file.dat


Code:
AFD|asdf|
asdf|ADF|
ASD|ASDF|ASDF|ASDFA|

and the output of the command is


Code:
AFD|asdf|asdf|ADF|ASD|ASDF|ASDF|ASDFA|

Sponsored Links
    #6  
Old 08-12-2005
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts

Code:
# cat file.txt
|a|b
c|d|
|e|f|i|
|h|i|j|


sed 'N; s/\n[       ]*\([^|]\)/\1/g' file.txt
|a|bc|d|
|e|f|i|
|h|i|j|

Sponsored Links
    #7  
Old 08-18-2005
shihabvk shihabvk is offline
Registered User
 
Join Date: May 2005
Last Activity: 14 July 2009, 8:56 AM EDT
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a loooooot!!!!!
My problem have been solved....

The Solution Suggested by Unbeliever is working fine....
When I tried with the solution by jerardfjay , it gives me the following error : Segmentation fault(coredump) .. Could you please tell me why ? I am using AIX 5.

The solution with sed by reborg is also working fine.. but it fails , if more that one \n is there in a single line. It appends the first with second..and so on... any way could you please explain the code.. if you don't mind

Thanks once again
Shihab
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove new line character at end of file. varun940 Shell Programming and Scripting 20 04-13-2012 02:21 AM
remove line feeds followed by character fluffdasheep Shell Programming and Scripting 5 07-07-2011 11:06 AM
How to remove new line character and append new line character in a file? sasikari HP-UX 8 10-27-2010 06:09 PM
sed: remove first character from particular line hkansal Shell Programming and Scripting 6 06-11-2009 02:47 AM
Remove Last Character of Line danhodges99 Shell Programming and Scripting 4 05-21-2003 09:30 AM



All times are GMT -4. The time now is 03:32 AM.