sed - Search and replace within pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - Search and replace within pattern
# 1  
Old 08-10-2015
Scissors sed - Search and replace within pattern

Hi Guys!

Unix newbie here!

Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far Smilie

I have a file which contains the following:-

Code:
TEST1|TEST2|"TEST3|1@!2"|TEST5

My sed command should result in either one the following output:-
Code:
TEST1|TEST2|TEST312|TEST5
TEST1|TEST2|"TEST312"|TEST5

The requirement is that I replace all non-alphanumeric characters within the double quotes "" with either a certain character or have that character removed.

Would really appreciate if any of you unix/sed gurus could guide me on correct path of having this sorted.

Zz

Last edited by vgersh99; 08-10-2015 at 12:01 PM.. Reason: code tags, please!
# 2  
Old 08-10-2015
something to start with:
Code:
awk -F'"' '{gsub("[^A-Za-z0-9]","",$2)}1' OFS='"' myFile

# 3  
Old 08-10-2015
Hi, try:
Code:
awk '{for(i=2; i<=NF; i+=2) gsub(/[^[:alnum:]]/,x,$i)}1' FS=\" OFS=\" file

or
Code:
awk '{for(i=2; i<=NF; i+=2) gsub(/[^[:alnum:]]/,x,$i)}1' FS=\" OFS= file

You could also try:
Code:
awk 'NR%2-1{gsub(/[^[:alnum:]]/,x)}1' RS=\" ORS= file


Last edited by Scrutinizer; 08-11-2015 at 12:22 AM..
# 4  
Old 08-10-2015
This code, inspired by the O'Reilly book "sed & awk" might help:
Code:

 # Transform quoted text to uppercase.
 /".*"/{
 h
 s/.*"\(.*\)".*/\1/
 y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
 G
 s/\(.*\)\n\(.*\)".*"\(.*\)/\2"\1"\3/
 }
 

By the time the code reaches the "y" command there is nothing in the pattern space other than quoted text. Replace the "y" line with your own "s" command. The last two lines recreate the whole line except for the quoted part you changed is inserted.

HTH

P.S. You might want an "s" command (instead of the "y") like
Code:
s/[^A-Z]/?/g

to replace all non (uppercase) alphabetic characters with ? or just omit the "?" to replace with nothing.

Last edited by wbport; 08-10-2015 at 05:53 PM.. Reason: Add P.S.
This User Gave Thanks to wbport For This Post:
# 5  
Old 08-11-2015
@wbport. Nice alternate approach. I think s/[^A-Z]//g should be replaced with: s/[^[:alnum:]]//g.
Also, s/\(.*\)\n\(.*\)".*"\(.*\)/\2"\1"\3/ can be simplified to s/\(.*\)\n\(.*\)".*"/\2\1/.

One caveat is that is does not work properly with multiple quoted texts on a line. For that it would need a loop with a conditional branch after that second substitution and it is easiest to achieve if the quotes are removed in the end result, so that it can work its way from back to front..
# 6  
Old 08-11-2015
Thanks loads for all your replies!! Will be trying them out and will let you know. Looking for the most simplistic/least processor intensive way of getting it done.

Regards,
Zz
# 7  
Old 08-12-2015
Code:
$ echo 'TEST1|TEST2|"TEST3|1@!2"|TEST5' | sed -e :x -e 's/"\([[:alnum:]]\{1,\}\)[^"[:alnum:]]/"\1/g; tx'
TEST1|TEST2|"TEST312"|TEST5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

search & replace pattern

Hi, My problem is that I have to search a changing pattern and replace it with the wild card char "*" i/p: 99_*_YYYYMMDD_SRC.txt.tar.gz o/p: 99_*_*_SRC.txt.tar.gz The problem is that YYYYMMDD pattern is not static. It could be YYYYMMDDHHMI or could be YYYYMMDDHHMISS. Can... (10 Replies)
Discussion started by: dips_ag
10 Replies

4. UNIX for Dummies Questions & Answers

pattern containg ' search and replace

Hi guys I'm new to this forum so please help me in this I have a file where i need to replace a pattern value=' ' with the pattern value='abc' and moreover that abc value must be passed from some variable say i assign name=abc and use name as the value to replace instead of the direct string... (10 Replies)
Discussion started by: sundarj
10 Replies

5. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

6. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

7. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

8. Shell Programming and Scripting

search replace pattern containing slashes

Need help in finding pattern then replacing pattern that contains multiple slashes .. ex . <imgp src="Attention_web.eps.jpg" align="left"> <imgp src="NewToday062308.eps.jpg"> replace with <imgp src="/ww2/adpay/liner/Attention_web.eps.jpg" align="left"> <imgp... (2 Replies)
Discussion started by: aveitas
2 Replies

9. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

10. Shell Programming and Scripting

Search for a Pattern and Replace

Hello All, Can you help me with this , I need to search a pattern replace it with the new pattern in all the files in a directory. what would be the easiest way to do that? Thanks in advance. :) Sam, (6 Replies)
Discussion started by: sbasetty
6 Replies
Login or Register to Ask a Question