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 delimeter between quotes with \| using sed or awk

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-04-2015   -   Original Discussion by BrahmaNaiduA
BrahmaNaiduA's Unix or Linux Image
BrahmaNaiduA BrahmaNaiduA is offline
Registered User
 
Join Date: Jan 2015
Last Activity: 23 February 2016, 12:46 AM EST
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Linux Replace delimeter between quotes with \| using sed or awk

I've a pipe delimited data in a file of size 3GB. if the text data conatins pipe delimiter that will be enclose with double quotes. I have to replace delimiter which exists between double quotes with #%@#%@#%@ using awk or sed. can some one provide a better and efficient solution to me.
The below snippet code which I used is taking longer time to execute the file.
.



Code:
awk {if(NR%2-1)gsub(/\|/,"#%@#%@#%@"); else if(!NF) $0=RS $0}1 RS=" ORS=



Code:
123456789|"SYN|THESIS MED CHEM PTY. LTD."|C||100.00|22|"AB""C|Corp"|"""XYZ"|"CDEF"""|""|""'

Result:


Code:
123456789|"SYN\|THESIS MED CHEM PTY. LTD."|C||100.00|22|"AB""C\|Corp"|"""XYZ"|"CDEF"""|""|""'


Last edited by Scrutinizer; 05-04-2015 at 03:49 PM.. Reason: code tags (mod:also for script)
Sponsored Links
    #2  
Old Unix and Linux 05-04-2015   -   Original Discussion by BrahmaNaiduA
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 January 2018, 5:23 AM EST
Location: Amsterdam
Posts: 11,709
Thanks: 530
Thanked 3,400 Times in 2,996 Posts
Try:


Code:
awk '!(NR%2){gsub(/\|/,"\\|")}1' RS=\" ORS=\" file

Sponsored Links
    #3  
Old Unix and Linux 05-04-2015   -   Original Discussion by BrahmaNaiduA
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 January 2018, 5:38 AM EST
Location: Aachen, Germany
Posts: 11,963
Thanks: 354
Thanked 3,688 Times in 3,386 Posts
The specification that you give and the sample result are not in agreement, and I doubt the code snippet you present does anything as it hat syntax and semantic errors. Would this come near to your desired result:

Code:
awk -F\" '{for (i=2; i<=NF; i+=2) gsub(/\|/,"#%@#%@#%@",$i)} 1' OFS=\" file
123456789|"SYN#%@#%@#%@THESIS MED CHEM PTY. LTD."|C||100.00|22|"AB""C#%@#%@#%@Corp"|"""XYZ"|"CDEF"""|""|""

    #4  
Old Unix and Linux 05-04-2015   -   Original Discussion by BrahmaNaiduA
BrahmaNaiduA's Unix or Linux Image
BrahmaNaiduA BrahmaNaiduA is offline
Registered User
 
Join Date: Jan 2015
Last Activity: 23 February 2016, 12:46 AM EST
Posts: 18
Thanks: 4
Thanked 0 Times in 0 Posts
Linux Repalce pipe between double quotes with \| using sed or awk comamnd



Code:
my code snippet is working fine. PFB result.
awk '{if(NR%2-1)gsub(/\|/,"#%@#%@#%@"); else if(!NF) $0=RS $0}1' RS=\" ORS= file.txt |sed -e 's/[\"]$//' -e 's/[ \t]*|[ \t]*/|/g' -e '/^$/d'
123456789|SYN#%@#%@#%@THESIS MED CHEM PTY. LTD.|C||100.00|22|AB"C#%@#%@#%@Corp|"XYZ|CDEF"||

But it's taking longer time when I ran a big file.

Please help me out.

---------- Post updated at 04:35 PM ---------- Previous update was at 04:33 PM ----------



Code:
The one which you shared is skipping the characters which doesn't have double quotes

Quote:
Originally Posted by Scrutinizer View Post
Try:


Code:
awk '!(NR%2){gsub(/\|/,"\\|")}1' RS=\" ORS=\" file
Sponsored Links
    #5  
Old Unix and Linux 05-04-2015   -   Original Discussion by BrahmaNaiduA
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 January 2018, 5:23 AM EST
Location: Amsterdam
Posts: 11,709
Thanks: 530
Thanked 3,400 Times in 2,996 Posts
Quote:
Originally Posted by BrahmaNaiduA View Post
[..]
The one which you shared is skipping the characters which doesn't have double quotes
I thought that was the idea ... If not, then what exactly are you looking for? Please be specific, there is a contradiction between your written requirement in post #1 and the output sample you provide in post #1...

Last edited by Scrutinizer; 05-04-2015 at 06:02 PM..
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
sed command to replace string that contain blackslash,double quotes thanush9sep Shell Programming and Scripting 6 11-11-2014 04:09 PM
awk to find and replace Double quotes between pipes krux_rap Shell Programming and Scripting 5 09-16-2014 01:20 AM
HELP with AWK or SED. Need to replace the commas between double quotes in CSV file shell_boy23 Shell Programming and Scripting 5 07-12-2012 05:54 AM
Replace double double quotes using AWK/SED Bhuvaneswari Shell Programming and Scripting 2 08-17-2011 08:04 AM
sed replace spaces between quotes with a variable EXT3FSCK Shell Programming and Scripting 3 03-07-2010 01:22 AM



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