Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-04-2015
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)
# 2  
Old 05-04-2015
Try:
Code:
awk '!(NR%2){gsub(/\|/,"\\|")}1' RS=\" ORS=\" file

# 3  
Old 05-04-2015
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
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
Try:
Code:
awk '!(NR%2){gsub(/\|/,"\\|")}1' RS=\" ORS=\" file

# 5  
Old 05-04-2015
Quote:
Originally Posted by BrahmaNaiduA
[..]
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..
Login to Reply

« Previous Thread | Next Thread »
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
Replace space within quotes SkySmart Shell Programming and Scripting 2 11-14-2017 02:12 AM
Replace every second instance of delimeter Sumanthsv Shell Programming and Scripting 4 03-01-2016 02:25 AM
Replace Double quotes within double quotes in a column with space while loading a CSV file mlavanya Shell Programming and Scripting 6 05-12-2015 12:05 AM
Replace space and tab to pipe delimeter greenworld123 Shell Programming and Scripting 7 04-25-2013 07:11 AM
Replacing the delimeter with other delimeter i150371485 Shell Programming and Scripting 4 08-07-2012 06:06 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 a value in the file that start with quotes chandu123 UNIX for Advanced & Expert Users 6 06-26-2012 04:44 PM
How to replace double quotes in to ascii value? rajesh4851 UNIX for Dummies Questions & Answers 4 05-11-2011 03:27 AM
Count the delimeter from a file and delete the row if delimeter count doesnt match. Akumar1 Shell Programming and Scripting 5 08-31-2010 11:41 PM
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:20 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password