×
UNIX.COM Login
Username:
Password:  
Show Password





×
UNIX.COM Login
Hello Unregistered
Welcome from United States
You are somewhere around Ashburn
Last Visit: 1 Minute Ago @ 03:19 PM
Bits: 0
UNIX Time: 1531682393

👤


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

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 02:49 PM.. Reason: code tags (mod:also for script)
Sponsored Links
    #2  
Old 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: 14 July 2018, 11:07 PM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
Try:


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

Sponsored Links
    #3  
Old 05-04-2015   -   Original Discussion by BrahmaNaiduA
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 15 July 2018, 1:38 PM EDT
Location: Aachen, Germany
Posts: 13,051
Thanks: 444
Thanked 4,004 Times in 3,681 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 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 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: 14 July 2018, 11:07 PM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 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 05:02 PM..
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:09 PM
awk to find and replace Double quotes between pipes krux_rap Shell Programming and Scripting 5 09-16-2014 12: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 04:54 AM
Replace double double quotes using AWK/SED Bhuvaneswari Shell Programming and Scripting 2 08-17-2011 07:04 AM
sed replace spaces between quotes with a variable EXT3FSCK Shell Programming and Scripting 3 03-07-2010 12:22 AM



All times are GMT -4. The time now is 03:19 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.