The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
to replace unwanted new line characters Raghava Shell Programming and Scripting 3 08-06-2008 03:34 AM
help to replace extra new line characters Raghava UNIX for Dummies Questions & Answers 3 08-04-2008 11:57 AM
Want to replace characters arndorff Shell Programming and Scripting 5 01-29-2008 04:05 PM
How to replace characters 7 through 14 of every line in a file jakSun8 Shell Programming and Scripting 9 12-13-2007 02:13 AM
Replace Characters... lgardner17325 UNIX for Dummies Questions & Answers 8 10-26-2006 06:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2008
Raghava Raghava is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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 (permalink)  
Old 08-05-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
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 (permalink)  
Old 08-05-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,448

Code:
 tr '\n' ' ' < file | sed 's/"  /"\
/g'


Last edited by danmero; 08-05-2008 at 10:50 AM.. Reason: Add another space
  #4 (permalink)  
Old 08-05-2008
Raghava Raghava is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
Hi JOYEG
please find attatchement as ZIP file.
Thanks
Attached Files
File Type: zip Book1.zip (852 Bytes, 7 views)
  #5 (permalink)  
Old 08-05-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 08-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0