Google Answers Logo
View Question
 
Q: UNIX text processing ( Answered,   1 Comment )
Question  
Subject: UNIX text processing
Category: Computers > Programming
Asked by: philnj-ga
List Price: $2.00
Posted: 09 Aug 2006 10:29 PDT
Expires: 08 Sep 2006 10:29 PDT
Question ID: 754319
I have an ASCII file on a Solaris UNIX system.  Some lines in the file
end with a continuation character \ then a newline character then the
line continues on the next line.  Lines are never broken into more
than two lines.

I need a vi command or a sed command that will remove the continuation
character and concatenate the two lines into one full line (with a
newline at the end).
Answer  
Subject: Re: UNIX text processing
Answered By: bookface-ga on 09 Aug 2006 10:50 PDT
 
# if a line ends with a backslash, append the next line to it
 sed -e :a -e '/\\$/N; s/\\\n//; ta'


This file is invaluable for anybody ever needing to do any text
processing on a system containing sed:
http://www.student.northpark.edu/pemente/sed/sed1line.txt

If no exact match for what you want to do is there, you can generally
find a similar solution that'll put you well in the right direction.

Clarification of Answer by bookface-ga on 09 Aug 2006 10:51 PDT
Thanks for choosing Google! Answers!

Request for Answer Clarification by philnj-ga on 09 Aug 2006 11:12 PDT
Thanks for the extremely fast response. 

I am getting a space character where the continuation character (\)
was.  This must be removed.

This file has over 45000 line, so automated processing is critical.

Request for Answer Clarification by philnj-ga on 14 Aug 2006 05:54 PDT
Still waiting for clarification.  Can't rate the answer until then.  I
am getting a space character where the continuation character (\) was.
 Please modify the command to remove this extra space character.
Comments  
Subject: Re: UNIX text processing
From: frankcorrao-ga on 15 Aug 2006 14:42 PDT
 
Are you sure there was not already a space before the escaped newline
in the input data? This sed command will not replace the newline with
a space.  The
's/\\\n//' portion of the command means replace backslash and newline
with nothing.  If you copied this down instead of cut and paste it
make sure there is no space between the last two slashes of this part
of the command.

If this was a space before the escaped newline in the original input,
you can remove that one as well by changing the command very slightly
to:
sed -e :a -e '/\\$/N; s/ \\\n//; ta'

Note the space after "s/".

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy