I have a requirement in which i have strings in a flat file separated by pipe character as delimiter (|)
All strings are enclosed in double quotes
This delimiter can also be within the data string
I need to remove the double quotes from those data strings having the same delimiter within itself as the actual delimiter
For eg If the flat file has strings like
"abc"|"xyz"|"def|ghi"
I want to keep the last string ie ""def|ghi" intact
and remove the double quotes from remaining strings in the file which do not have a delimiter in between them
so my o/p will now look like
all = open("input.txt").read()
t = all.split('"')
t.pop(0) #remove first element
t.pop(-1) #remove last element
for num,i in enumerate(t):
if "|" in i and i != "|":
t[num] = '"' + i + '"'
print ''.join(t)
If the string
"stri"n"g" should be left unchanged then what is the change required
as all quotes within quotes should be intact as per requirement
secondly the sed command has overwritten the first basic requirement
ie. pipe(\) enclosed within strings should be untouched
"abc|def" should be unchanged
make sure you don't have any pattern like @# is there in your input. If it is there then make some pattern which is not your input and replace @# in above code with your pattern.
but in another application requirement is almost similar
with slight modifications the delimiter is the same ie pipe (|)
1) The record may have several null columns
2) strings with just opening and close quotes should be removed ,
if they don't have a pipe in between the quotes else quotes should remain as it is
3) If there are more than 2 quotes in the string between delimiters then there should be no change else remove the quotes