How to replace string between delimiters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace string between delimiters?
# 1  
Old 06-24-2016
How to replace string between delimiters?

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. 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

How could I do it using sed or awk?
Thanks in advance.
# 2  
Old 06-24-2016
Such things have been asked several times ago.
One of the frequently given answers was
Code:
awk 'NR%2==0 { gsub(";","|") } 1' RS='"' ORS='"' file

The trick is to set the Record Separator to ".
For clarification how the "lines" are seen do this
Code:
awk '1' RS='"' file

The rest is easy: on every second line replace all ; by |
# 3  
Old 06-24-2016
When I try MadeInGermany's code, I get an extraneous " at the end of the file and the resulting file is not a text file (since it is missing a trailing <newline> character after the extraneous ").

One possible alternative is:
Code:
awk '
BEGIN {	FS = OFS = ";"
}
{	for(i = 1; i <= NF; i++) {
		nq += gsub(/\"/, "&", $i)
		printf("%s%s", $i, (i == NF) ? "\n" : (nq % 2) ? "|" : OFS)
	}
}' file

which uses semicolons as the field separators and replaces field separators found inside double quotes with pipe symbols. If file contains:
Code:
abc;abc;"abc;abc";cd;"ef;ef";abc
aa;bb;"aa";cc;"ddd;eee";ff
a;"b";"c;d";"e;f;g"
a;"b";"c;d";"e;f;g";h

it produces the output:
Code:
abc;abc;"abc|abc";cd;"ef|ef";abc
aa;bb;"aa";cc;"ddd|eee";ff
a;"b";"c|d";"e|f|g"
a;"b";"c|d";"e|f|g";h

# 4  
Old 06-24-2016
Wouldn't just this do?
Code:
awk '{for (i=2; i < NF; i+=2) gsub (/\;/, "|", $i)} 1' FS=\" OFS=\" file
abc;abc;"abc|abc";cd;"ef|ef";abc
aa;bb;"aa";cc;"ddd|eee";ff
a;"b";"c|d";"e|f;g"
a;"b";"c|d";"e|f;g";h

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-24-2016
In sed:
Code:
sed 's/\("[a-z]*\);\([a-z]*"\)/\1|\2/g' file


Last edited by greet_sed; 06-24-2016 at 06:37 PM.. Reason: updated code
# 6  
Old 06-24-2016
Code:
perl -pe 's/(;"\w+);/$1|/g' bartleby.file

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

# 7  
Old 06-24-2016
Quote:
Originally Posted by greet_sed
In sed:
Code:
sed 's/\("[a-z]*\);\([a-z]*"\)/\1|\2/g' file

It works as long as there is no more than one semicolon between double quotes, but it doesn't work with input like:
Code:
a;"b";"c;d";"e;f;g"
a;"b";"c;d";"e;f;g";h

producing the output:
Code:
a;"b";"c|d";"e;f;g"
a;"b";"c|d";"e;f;g";h

instead of:
Code:
a;"b";"c|d";"e|f|g"
a;"b";"c|d";"e|f|g";h

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.
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