Replace newline character between a double quotes to a space

Hi Guys,

I have a file with content as below

aj.txt

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

"Iamallfine" abcdef
abcd "all is not well"

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

please help.

Regards,
Aj

Try:

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

It worked fine. It would be a great help if you can explain it a bit

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. :confused:

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"..

1 Like

If it is a matter of just joining every two lines, then this might work too:

paste -d " " - - < infile

oops an issue again !!

/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)

Could you post a more representative sample of STD_H.txt, anonymized if need be?

Issue Resolved :smiley:

first added "" at the end of each line in the file using

/usr/xpg4/bin/sed 's/$/""/g' $file >file2 

this basically avoids all too long records. As every line now has atleast 2 records, if " is considered as a Record selector.

Then executed:

/usr/xpg4/bin/awk '(NR-1)%2{$1=$1}1' RS=\" ORS=\" file2 > file3 

A \n was missing in the EOF as record selector was " and not \n. Remove the last line and appended a \n at the EOF using:

printf %s\\n d w q | ed -s file3

And Finally Replaced All "" with a blankspace using

/usr/xpg4/bin/sed 's/""/ /g' file3 > file4

I Actually didn't create file2 and file3 just used a pipe

|

instead. Just gave it here to be more clear. Had to create file4 as -i option is not present in

/usr/xpg4/bin/sed

Any Suggestions to make it simpler ?

Regards,
Ajay