I have below tst.csv with the below content
"FORESS INTL PTN "A" (208)"
Need to get the below o/p and the reqmt is we should not be using any escape charecter () and also gsub/sub only needs to be used to acheive the below expected o/p - as external program that we are using is not be decode the backslash charecter.
Expected o/p
"FORESS INTL PTN ""A"" (208)"
Tried the below cmd
awk '{gsub(/"/,"&&");gsub(/^""|""$/,"")}1' tst.csv
I get the below o/p
FORESS INTL PTN ""A"" (208)
But not able to get double quotes in front of the text nor at the end of the line.
@reach2khan , you could use an awk variable and print it simply.
awk -v s1="\"" '{$(NF-1)=s1 $(NF-1) s1} 1' Input_file
Here I have created variable s1
which has value as "
, I am adding it to 2nd last item here.
Thanks,
R. Singh
1 Like
Try to \escape the literal " within the " "
awk '{gsub(/"/,"&&");gsub(/^""|""$/,"\"")}1' tst.csv
1 Like
Hi
I see that all arguments must be in quotation marks. I suggest the option if the string is a little more complicated
sed -r 's/"[^"]*"/&\r/g;s/\r([^"]+)/"\1"/g;s/^[^"]+/"&"/;s/\r//g' tst.csv
Or try something like this:
awk '{gsub(/"/,"&&",$2)}1' FS='^"|"$' OFS=\" file
or
perl -pe 's/(?<!^)"(?!$)/""/g' file
1 Like
@reach2khan , One more solution a Generic one, this will check all fields which are covered with "
will add extra "
before and after its value.
awk -v s1="\"" '{for(i=1;i<=NF;i++){if($i~"^"s1".*"s1"$"){$i=s1 $i s1}}} 1' Input_file
Thanks,
R. Singh
1 Like
The main thing is optimization!
sed -r 's/(^")?([^"]+)"*/"\2"/g' tst.csv
1 Like
system
Closed
August 28, 2020, 11:01am
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.