How to replace string between delimiters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace string between delimiters?
# 8  
Old 06-24-2016
Quote:
Originally Posted by Don Cragun
[...]
and Aia's perl script produces:
Code:
a;"b";"c|d";"e|f;g"
a;"b";"c|d";"e|f;g";h

with the same input.
By design. Based on OP example and words; not based on your hypothetical input.
You should start a new thread if you would like me to provide a solution for you, addressing your input.

Last edited by Aia; 06-24-2016 at 10:39 PM..
# 9  
Old 06-25-2016
Quote:
Originally Posted by Aia
By design. Based on OP example and words; not based on your hypothetical input.
You should start a new thread if you would like me to provide a solution for you, addressing your input.
It is my belief that the original request was to change semicolon delimiters inside double-quoted strings to vertical bar delimiters. But, the given examples did not include a case where more than on delimiter appeared inside double quotes. Therefore, the original request is not as clear as I would like.

I did not ask you to make any changes.

I see no reason to start a new thread when pointing out to the original poster that the code suggested by you, the code suggested by greet_sed, and the code suggested by me (and refined to a better suggestion by RudiC) do three different things if there is more than one semicolon inside double quotes: Your suggestion changes the 1st semicolon, greet_sed's suggestion doesn't change anything, and my and RudiC's suggestion changes every semicolon.

I believe it is entirely up to bartleby to determine if any of this matters in the real input he/she will be processing (instead of the contrived 2-line sample input) and, if it does matter, to choose the suggestion that behaves the way bartleby wants it to behave. But, I believe that bartleby should be made aware in this thread that the three suggestions do not do the same thing and should be warned to determine whether the differences in our solutions matter.
# 10  
Old 06-25-2016
Quote:
Originally Posted by Don Cragun
It is my belief that the original request was to change semicolon delimiters inside double-quoted strings to vertical bar delimiters. But, the given examples did not include a case where more than on delimiter appeared inside double quotes. Therefore, the original request is not as clear as I would like.
I do not doubt in your belief. However, by your own words, your belief made you assume with what you were not clear about, to start with.
I chose to interpret the request as stated:
Quote:
Originally Posted by bartleby
Hello,
I would need to replace a delimiter in a flat file using.I would like to replace the semicolon (";") but only if it was contained in a string between quotes
One semicolon. Single semicolon reference, in both instances, highlighted in red, follow by specific post:
Quote:
Originally Posted by bartleby
For example:

Original flat file example:
Code:
abc;abc;"abc;abc";cd;"ef;ef";abc
aa;bb;"aa";cc;"ddd;eee";ff

Desired output:
Code:
abc;abc;"abc|abc";cd;"ef|ef";abc
aa;bb;"aa";cc;"ddd|eee";ff

It was clear to me as stated. My solution was designed based on the given example, not based on requirements conceived by me. Perhaps, it would had been more understandable if you were to ask for direct clarification to the OP, excluding other suggestions not given by you. Instead of giving an audit of other suggestions, based on your assumptions and leaving there to anyone to interpret.

Quote:
Originally Posted by Don Cragun
Your suggestion changes the 1st semicolon
Not quite accurate neither. You are still declaring it based on your belief.
My suggestion will replace any semicolon which precedes another semicolon, followed by a literal double quotes, followed by one or more alpha-numberic characters. Which will be applicable to the shown example and it will not provide the expected results if the shown examples are not representative. If that's the case, the OP will communicate back if he or she is interested on the suggestion.

Last edited by Aia; 06-25-2016 at 05:53 PM.. Reason: grammar correction.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

2. UNIX for Dummies Questions & Answers

Replace Delimiters with Space

Hi All, How to Replace the delimiter for a particular field. I have used awk to replace the field values based on the position, but I tried to remove/replace delimiters with space on particular positions. I tried tr command with combination of awk not sure if this is the correct way, but I am... (3 Replies)
Discussion started by: mora
3 Replies

3. Shell Programming and Scripting

Extract value between the delimiters and replace it with another value

Hi All, i have file name like below ABC_065224_123456_123456_your_130413_163005.txt ABC_065224_123456_MAIN_20130413_163005.txt ABC_065224_123456_123456_MAIN_130413_163005.txt ABC_065224_123456_123456_434567_MAIN_130413_163005.txt i need to find out the number of characters in the filed... (6 Replies)
Discussion started by: dssyadav
6 Replies

4. Shell Programming and Scripting

How to Replace string between delimiters?

Basically , i want to delete strings of a particular pattern from the flat file which is " | " pipe delimited. Below are the valid formats : 1) AAA (0) 111-111-111, AAA, BB 2) AAA (0) 111-111-1111;X, AAA, BB original flat file example : |ABC ABC XHAMK|AAA (0) 111-111-111, AAA,... (3 Replies)
Discussion started by: Ravi_007
3 Replies

5. Shell Programming and Scripting

How to parse a numeric string without any delimiters?

Hi , I have a number say 12345001 which needs to be parsed. Its a number that has no delimiters.I have to read the last three digits and then the rest of digits irrespective of the total length of the number. The digits then have to be swapped and changed to a fixed length. The fillers to be... (10 Replies)
Discussion started by: Sheel
10 Replies

6. Shell Programming and Scripting

Getting a string without fixed delimiters

I have a line of text for example aaaa bbbb cccc dddd eeee ffffff I would need to get the cccc however bbbb could be there or not. So whether bbbb is in the line or not I need cccc. I was looking at either awk or sed....and trying to start at c and end until the next space. Also... (11 Replies)
Discussion started by: bombcan1
11 Replies

7. UNIX for Dummies Questions & Answers

Delete string between delimiters with sed

Hi, I need to delete all text between "|" delimiters. The line in text file typically looks like this: 1014182| 13728 -rw-r--r-- 1 imac1 staff 7026127 2 okt 2010 |/Users/imac1/Music/iTunes/iTunes Media/Music/Various Artists/We Are the World_ U.S.A. for Africa/01 We Are the World.mp3... (2 Replies)
Discussion started by: andrejm
2 Replies

8. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

9. Shell Programming and Scripting

string deletion, variable contents, fixed delimiters

Hi, I need to mass delete the following string(s) from my files weight=100, However the '100' is variable e.g Current: ---------------- moretext=bar, weight=100, moreinfo=blah extrastuff=hi, weight=9999, extrainfo=foo Desired: ------------------ moretext=bar, moreinfo=blah... (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

10. Shell Programming and Scripting

Using sed to delete string between delimiters

Hi There! I have the following string which i need to convert to i.e. between each occurence of the delimiter ('|' in this case), i need to delete all characters from the '|' to the ':' so that |10,9:12/xxx| becomes |12/xxx| How can i do this using sed? Thanks in advance! (13 Replies)
Discussion started by: orno
13 Replies
Login or Register to Ask a Question