Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replace newline character between a double quotes to a space

Shell Programming and Scripting


Tags
awk, grep, replace newline, sed, sed awk grep replace newline

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
ajahuja ajahuja is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 27 October 2017, 6:55 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Error Replace newline character between a double quotes to a space

Hi Guys,

I have a file with content as below

aj.txt


Code:
"Iam
allfine" abcdef
abcd "all is 
not well"

What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text.

output must be:

Code:
"Iamallfine" abcdef
abcd "all is not well"

the machine has SUN OS in it. only korn shell.

please help.

Regards,
Aj

Last edited by Scrutinizer; 08-01-2012 at 03:18 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2017, 8:17 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 518
Thanked 3,381 Times in 2,980 Posts
Try:

Code:
/usr/xpg4/bin/awk '(NR-1)%2{$1=$1}1' RS=\" ORS=\" infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
ajahuja (08-01-2012)
Sponsored Links
    #3  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
ajahuja ajahuja is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 27 October 2017, 6:55 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
It worked fine. It would be a great help if you can explain it a bit
    #4  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
ajahuja ajahuja is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 27 October 2017, 6:55 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
It worked !!
Attached are the Screenshots!
onlt thing is ther is an extra line in the output with a " in it

please can you explain it. I didn't understand the code. Linux
Attached Thumbnails
Replace newline character between a double quotes to a space-my-tabdelimited-file-jpg   Replace newline character between a double quotes to a space-after-command-jpg  

Last edited by ajahuja; 08-01-2012 at 05:25 PM..
Sponsored Links
    #5  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2017, 8:17 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 518
Thanked 3,381 Times in 2,980 Posts
Yes, I guess it may appear a bit cryptic..

It uses a double quote as input and output record selector (RS=\" ORS=\"). So any record is either inside or outside double quotes. The mod of the line number divided by 2 ( (NR-1)%2 ) determines which is the case. If it is the case then $1=$1 is used to replace any occurrence of the default input field selector (FS), i.e. any combination of consecutive spaces, TABs or newlines to be replaced by the default output field selector (OFS), which is a single space...
The 1 is synonymous for "print the record"..
The Following User Says Thank You to Scrutinizer For This Useful Post:
ajahuja (08-01-2012)
Sponsored Links
    #6  
Old Unix and Linux 08-01-2012   -   Original Discussion by ajahuja
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2017, 8:17 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 518
Thanked 3,381 Times in 2,980 Posts
If it is a matter of just joining every two lines, then this might work too:

Code:
paste -d " " - - < infile

Sponsored Links
    #7  
Old Unix and Linux 08-02-2012   -   Original Discussion by ajahuja
ajahuja ajahuja is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 27 October 2017, 6:55 PM EDT
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
Error

oops an issue again !!



Code:
/usr/xpg4/bin/awk '(NR-1)%2{$1=$1}1' RS=\" ORS=\" STD_H.txt > STD_TRUE.txt

/usr/xpg4/bin/awk: line 0 (NR=1561): Record too long (LIMIT: 19999 bytes)

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using sed I want to replace space by newline RahulJoshi Shell Programming and Scripting 5 09-21-2010 10:04 AM
Removal of new line character in double quotes vsairam Shell Programming and Scripting 7 05-19-2010 04:44 PM
newline character, space and tab after a string angelina UNIX for Advanced & Expert Users 6 07-11-2008 12:10 PM
How can I replace newline character? ghoti UNIX for Dummies Questions & Answers 1 08-16-2001 01:49 PM



All times are GMT -4. The time now is 01:37 AM.