Replace String matching wildcard pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace String matching wildcard pattern
# 1  
Old 12-04-2018
Replace String matching wildcard pattern

Hi,

I know how to replace a string with another in a file.

But, i wish to replace the below string pattern

EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>"

To

EncryptedPassword=""

i.e remove the random password to a empty string.

Can you please let me know how can I achieve this ?

Last edited by mohtashims; 12-04-2018 at 07:38 AM..
# 2  
Old 12-04-2018
This is a job for sed Smilie

Code:
sed -e 's/EncryptedPassword="[^"]*"/EncryptedPassword=""/'

This User Gave Thanks to derekludwig For This Post:
# 3  
Old 12-04-2018
works !! thank you
# 4  
Old 12-04-2018
Or with a back-reference; for example if you allow spaces around the = it will keep them.
Code:
sed 's/\(EncryptedPassword *= *\)"[^"]*"/\1""/'

In contrast to .* the [^"]* does a minimum match; important in case more "strings" follow it.
perl -pe is quite similar to sed (and even to awk); a minimum match can be elegantly done by a ? after a "greedy" *:
Code:
perl -pe 's/(EncryptedPassword *= *)".*?"/$1""/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace pattern matching

Can anyone help me with sed or awk to do a bulk replace of the below requirements. "REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH +7486 INCREMENT BY +1 MINVALUE +7467 MAXVALUE... (6 Replies)
Discussion started by: ilugopal
6 Replies

2. Programming

Wildcard Pattern Matching In C

I've been having problems lately trying to do pattern matching in C while implementing wildcards. Take for instance the following code: #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <dirent.h> #include <string.h> ... (14 Replies)
Discussion started by: Azrael
14 Replies

3. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

4. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

5. Shell Programming and Scripting

Pattern matching and replace in shell script

Hi I want to find a line in a file which contains a word and replace the patterns. Sample file content temp.xml ==================== <applications> <application> Name="FirstService" location="http://my.website.selected/myfirstService/V1.0/myfirst.war" ... (1 Reply)
Discussion started by: sakthi.99it
1 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Need help to replace a perl pattern matching

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (4 Replies)
Discussion started by: sags007_99
4 Replies

8. UNIX for Dummies Questions & Answers

sed non-greedy pattern matching with wildcard

Toby> cat sample1 This is some arbitrary text before var1, This IS SOME DIFFERENT ARBITRARY TEXT before var2 Toby> sed -e 's/^This .* before //' -e 's/This .* before //' sample1 var2 I need to convert the above text in sample1 so that the output becomes var1, var2 by... (2 Replies)
Discussion started by: TobyNorris
2 Replies

9. Shell Programming and Scripting

Find replace a particular string of data with wildcard

Hi I am having a csv file in which lots of data are available wherein i need to find a particular kind of data and replace it with null value. here is the sample data.. I need to find the string starting with 404-064- and up to the first space i have to remove the data and keep the... (4 Replies)
Discussion started by: aemunathan
4 Replies

10. Shell Programming and Scripting

use sed do batch wildcard string replace

Hi, Here is what I want to do I want to search local directory and its sub directory, all the files which contain any string like _12345, then remove this string. String is a combination of _ plus a random integer number. For example, here is one line in a file before <properties... (1 Reply)
Discussion started by: bp5000
1 Replies
Login or Register to Ask a Question