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 08-08-2012
Java 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 :
Code:
|ABC ABC XHAMK|AAA  (0) 111-111-111, AAA, BB|ABS ABS XYZ|
|ABC BBB CCC|AAA (0) 111-111-1111;X, AAA, BB|NNN XXX|...

Desired output:
Code:
|ABC ABC XHAMK|ABS ABS XYZ|
|ABC BBB CCC|NNN XXX|...



I am getting the output when i use the below:

Code:
sed 's/|AAA[]*([0-9]*)[]*[0-9]*-[0-9]*-[0-9]*, [A-Z]*, [A-Z]*//'

But i need some generic approach that can be used for all two valid format given above.
Also, the values (111-111-111) can be any thing like ALPHANUMERIC [[A-Z]*[0-9]* and special characters like [.(+&$*);-/,%:#@"]


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-08-2012 at 11:17 AM.. Reason: code tags
# 2  
Old 08-08-2012
sed does not understand columns, and getting it to do so through a complicated enough regex can be ugly.

awk does understand columns, and can have expressions with more than one regex in them.

Code:
awk -F"|" '($2 ~ /regex1/)||($2 ~ /regex2/) { 
        $2="";
        gsub(/[|][|]/, "|");
}' filename

# 3  
Old 08-09-2012
MySQL

Can you please explain the code that you provided. And how i can implement the same in my scenario?

---------- Post updated at 01:59 AM ---------- Previous update was at 01:49 AM ----------

The below command is useful but it aslo delete the other patterns starting with AAABCDE

Code:
 
sed -e "s/|AAA[ ]*[^|]*|/|/g"

Sample :
Code:
|ABC ABC XHAMK|AAA  (0) 111-111-111, AAA, BB|ABS ABS XYZ|
|ABC BBB CCC|AAACDE|NNN XXX|...

Output: Bu using sed -e "s/|AAA[ ]*[^|]*|/|/g"

Code:
 
|ABC ABC XHAMK|ABS ABS XYZ|
|ABC BBB CCC||NNN XXX|...

Here, even the string AAACDE is getting deleted , which is incorrect.

Desired Output :

Code:
|ABC ABC XHAMK|ABS ABS XYZ|
|ABC BBB CCC|AAACDE|NNN XXX|..


Only Pattern starting with AAA followed with one or more space.
Logic: Read the pipe "|" followed with pattern (|AAA (One or more space) till the other pipe is encounter and replace with the "|")
# 4  
Old 08-09-2012
Quote:
Originally Posted by Ravi_007
Can you please explain the code that you provided. And how i can implement the same in my scenario?
It uses awk with the field-separator of |.

It checks if the second field ($2) matches two different regexes, /regex1/ or /regex2/, and if it does, sets field 2 blank ($2="")

Then it replaces || with | in the entire line, to delete field 2 completely.

You'd implement it in your case by putting the regexes you wanted in /regex1/ and /regex2/.
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. Shell Programming and Scripting

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: abc;abc;"abc;abc";cd;"ef;ef";abc aa;bb;"aa";cc;"ddd;eee";ff Desired output:... (9 Replies)
Discussion started by: bartleby
9 Replies

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

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

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