Replace specific characters until pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific characters until pattern
# 1  
Old 10-22-2013
Replace specific characters until pattern

Hi experts,

My file looks something like this.

Code:
abcXX4,7,234
abc,defg,45XX23,74,123

The number of commas left of the XX can vary. The question is how can I replace all the commas left of the 'XX' with an underscore?

Code:
abcXX4,7,234
abc_defg_45XX23,74,123

Thanks!

Last edited by Scrutinizer; 10-22-2013 at 09:20 PM.. Reason: code tags
# 2  
Old 10-22-2013
Try:
Code:
awk '{gsub(/,/,"_",$1)}1' FS=XX OFS=XX file

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-23-2013
in bash:

Code:
while read a ; do echo ${a//,/_} ; done < file


Last edited by Scrutinizer; 10-23-2013 at 06:53 PM.. Reason: cide tags
# 4  
Old 10-23-2013
Quote:
Originally Posted by elbrand
in bash:

while read a ; do echo ${a//,/_} ; done < file
You missed the "until pattern" qualification.

Regards,
Alister
# 5  
Old 10-23-2013
Code:
sed -e ':a' -e '/,.*XX/ s/,/_/; ta' file

This is little different (replaces up to the rightmost XX), e.g. in this case
Code:
abc,defg,45XX23,1XX1,74,123

# 6  
Old 10-23-2013
Hi,
Just for fun, another sed solution but catch first XX occurence:
Code:
sed '/[^,]*XX/{;s//\n&/;h;s/\n.*$//;y/,/_/;G;s/\n.*\n//;}' file

Example:
Code:
$ echo 'abc,defg,45XX23,1XX1,74,123' | sed '/[^,]*XX/{;s//\n&/;h;s/\n.*$//;y/,/_/;G;s/\n.*\n//;}'
abc_defg_45XX23,1XX1,74,123

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Pattern with another that has Special Characters

Hello Team, Any help would be much appreciated for the below scenario: I have a sed command below where I am trying to replace the contents of 'old_pkey' variable with 'new_pkey' variable in a Soap request file (delete_request.txt). This works fine for regular string values, but this new_pkey... (8 Replies)
Discussion started by: ChicagoBlues
8 Replies

2. Shell Programming and Scripting

sed command on AIX, replace specific characters

Hi, Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed. the source data that I have is$cat v.txt 4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies

3. Shell Programming and Scripting

Replace Language Specific Characters in File

Hi Team, I have requirement to replace Language Specific Characters in File. We have set of characters, it should be replaced to a different character in the file. I have around 38 characters which should replaced to different destination character. Please help. Thanks Bharat (3 Replies)
Discussion started by: bharath561989
3 Replies

4. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 Replies

5. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

6. Shell Programming and Scripting

Replace the line with specific pattern

Hello All I'm trying to change one string from a file contening this patern: xxxx-xxxx 4 numbers - end 4 other numbers This is a sample of the file: LDR 00679 am a2200205 4500 =001 3617 =008 030219s2000\\\\xxx|||||\||||\00|\0\spa\d =020 \\$a0211-1942 =041 \\$aCastellà =093 ... (5 Replies)
Discussion started by: ldiaz2106
5 Replies

7. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

8. Shell Programming and Scripting

Help with replace the content of specific pattern

Input file: __<name>AWEETET</name> ____<name_evidence="3"_type="2@#">QEWQE</name> __<name>QWE048</name> ____<name_evidence="3"_type="570">@#@$#545</name> ____<name_evidence="2"_type="351">QWE4</name> Desired output: __<tmp>AWEETET</tmp> ____<name_evidence="3"_type="2@#">QEWQE</name>... (2 Replies)
Discussion started by: perl_beginner
2 Replies

9. Shell Programming and Scripting

find replace a pattern and following characters in a word

Suppose that I have a string "one:#red two:#yellow three:#gr'een four:#blu^e" and I want to replace the pattern :# and the following characters in the word with nothing. The output string should look "one two three four" How can I do this with sed. Some points to consider (a) the last word in... (1 Reply)
Discussion started by: superuser84
1 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies
Login or Register to Ask a Question