to replace un wanted new line characters in double quoats


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to replace un wanted new line characters in double quoats
# 1  
Old 08-05-2008
to replace un wanted new line characters in double quoats

Hi
my file data is like below

ramu,sony,"raju \n ravi \n ramya" \n
ravi,sarah,"sowmya \n sorry \n sangam" \n

i want replace new line characters in between double coats with single space.

for example
cat input_file

ramu,sony,"raju
ravi
ramya"
ravi,sarah,"sowmya
sorry
sangam"

which i dont want. i want out put like below.i want replace newline s in between double quoates.

ramu,sony,"raju ravi ramya"
ravi,sarah,"sowmya sorry sangam"

any body can help either perl script or any other script.

but JOEYG given follwoing solution.
it is not giving correct data

> cat new_lines | sed 's/" /" ~/' | tr "\n" " " | tr "~" "\n"

with above script iam getting out put in single line as below
ramu,sony,"raju ravi ramya" ravi,sarah,"sowmya sorry sangam"

but i want out put like below.

ramu,sony,"raju ravi ramya"
ravi,sarah,"sowmya sorry sangam"
# 2  
Old 08-05-2008
Question Thought my solution would've worked

Can you attach the datafile? I am having a difficult time undertanding the spacing of certain characters; also, trying to get at those floating <new-line> characters is difficult.

To attach a file here, use the paperclip icon.
# 3  
Old 08-05-2008
Code:
 tr '\n' ' ' < file | sed 's/"  /"\
/g'


Last edited by danmero; 08-05-2008 at 10:50 AM.. Reason: Add another space
# 4  
Old 08-05-2008
Hi JOYEG
please find attatchement as ZIP file.
Thanks
# 5  
Old 08-05-2008
Hammer & Screwdriver Didn't see exact example in attached file

I think the issue from my script, and possibly other proposed solutions, has to do with the spacing around double-quotes and <new-line> characters.

See the following example coding. To begin with, made the file with a # in place of the <new-line> or \n character. Verify it is there, and then translate to get the charcter in the new file

Code:
> cat new_lines
ramu,sony,"raju # ravi # ramya" # 
ravi,sarah,"sowmya # sorry s\ sangam" # 
> cat new_lines | tr "#" "\n"
ramu,sony,"raju 
 ravi 
 ramya" 
 
ravi,sarah,"sowmya 
 sorry s\ sangam" 
 
> cat new_lines | tr "#" "\n" >new_lines2

Now, I enter commands to grab the " that is followed by a space. In your example and my file, there is a space character after the closing " character. Hence, why the script works.

Code:
> cat new_lines2 | sed 's/" /" ~/' | tr "\n" " " | tr "~" "\n" | tr -s " "
ramu,sony,"raju ravi ramya" 
 ravi,sarah,"sowmya sorry s\ sangam"

# 6  
Old 08-06-2008
How about Perl:

Code:
vnix$ perl -0777 -pe '1 while s/("[^"\n]+)\n([^"]+"\r(?:$|\n))/\1 \2/' Book1.csv 
Mohan,Mohan,"Hello Hello Test"
Mohan,Mohan,"Hello Hello Test1"
Mohan,Mohan,"Hello Hello Test2"

Why does the attachment contain a lot of commentary? At least please put the test data in a separate file inside the zip so we can see what's what. As you can see, I assumed the test material was in the first three lines of the file.

The test data had DOS CRs (\r) where the "real" newlines are supposed to be, but not in other places; knowing that from the start would have simplified the discussion a lot. If that's not really so then take out the \r in the script above.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to replace new line characters in a huge file

Hi , I would like to replace new line characters(\n) in a huge file of about 2 million records . I tried this one (:%s/\n//g) but it's hanging there and no result. Does this command do not work if the file is big. Please let me know if you have any other options Regards Raj (1 Reply)
Discussion started by: rajeevm
1 Replies

2. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

3. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

4. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

5. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

6. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

7. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

8. Shell Programming and Scripting

to replace unwanted new line characters

Hi how to replace un wanted new line characters. my file contains data like. ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry s\ sangam" \n i want replace new line characters in between double coats with single space. for example $ cat input_file ramu,sony,"raju... (3 Replies)
Discussion started by: Raghava
3 Replies

9. UNIX for Dummies Questions & Answers

help to replace extra new line characters

Hi my file data is like below ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry s\ sangam" \n i want replace new line characters in between double coats with sinhle space. for example cat input_file ramu,sony,"raju ravi ramya" ravi,sarah,"sowmya sorry sangam" ... (3 Replies)
Discussion started by: Raghava
3 Replies

10. Shell Programming and Scripting

How to replace characters 7 through 14 of every line in a file

Hi all, I have a file with multiple lines. I want to replace characters 7 through 14 of every line with 0000000 Input: 12345678901234567890 23456789012345678901 Output 12345600000004567890 23456700000005678901 Please help. JaK (9 Replies)
Discussion started by: jakSun8
9 Replies
Login or Register to Ask a Question