String replacement when particular pattern matches in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String replacement when particular pattern matches in a file
# 1  
Old 12-22-2011
Data String replacement when particular pattern matches in a file

I have a file file123.xml which looks like this

xmlEntry="username"="josh" <property="never_back_down">
phone="<178652>"
apn=property:address="wonderland"
xmlEntry="username"="jessica" <property="never_back_down">
phone="<178653>"
apn=property:address="wonderland"
xmlEntry="username"="linda" <property="never_back_down">
phone="<178654>"
apn=property:address="wonderland"
xmlEntry="username"="jamie" <property="never_back_down">
phone="<178655>"
apn=property:address="wonderland"
phone="<178656>"
xmlEntry="username"="john" <property="never_back_down">
phone="<178657>"
apn=property:address="wonderland"
--------------------------------------------
Requirement is to whenever the following pattern is matched
xmlEntry="username"="<NAME>" <property="never_back_down">
the <NAME> will be replaced by <DUMMY_NAME>
e.g.
1.
xmlEntry="username"="jessica" <property="never_back_down">
will be replaced by
xmlEntry="username"="DUMMY_jessica" <property="never_back_down">
2.
xmlEntry="username"="john" <property="never_back_down">
will be replaced by
xmlEntry="username"="DUMMY_john" <property="never_back_down">

Thanks in advance.
Regards,
Poga
# 2  
Old 12-22-2011
How about this

Code:
sed -e 's/"jessica"/"DUMMY_jessica"/' -e 's/"john"/"DUMMY_john"/' file123.xml

# 3  
Old 12-22-2011
Code:
$ awk -F\" 'BEGIN{OFS="\""} {if($0~/username/) {$4="DUMMY_"$4;print $0}else{print $0}}' file123.xml > output.xml

use nawk if it is solaris
# 4  
Old 12-22-2011
I am looking for the all the lines that has xmlEntry="username"="<NAME>" <property="never_back_down"> to be replaced by xmlEntry="username"="<DUMMY_NAME>" <property="never_back_down">, not only for the specific names. Smilie
# 5  
Old 12-22-2011
Code:
$ sed 's/username"="/&DUMMY_/' test.txt

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 12-22-2011
Thanks Itkamaraj , it worked. Thanks to you too Shawn for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

4. Shell Programming and Scripting

Unix file pattern check and replacement

HI Guys , Using UNIX ,I intend to check with correct file pattern Access_file_Record.YYYYMM in path /tmp If the file exist in correct format come out from code . If not found check with different file patterns for same month period YYYYMM ( Like Access_file_Record_YYYYMM.txt or... (8 Replies)
Discussion started by: Perlbaby
8 Replies

5. Shell Programming and Scripting

script to delete lines from a txt file if pattern matches

File 6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04 42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36 6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies

6. Shell Programming and Scripting

Merge lines from one file if pattern matches

I have one comma separated file (a.txt) with two or more records all matching except for the last column. I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily? I've searched the forum but most talked... (6 Replies)
Discussion started by: giannicello
6 Replies

7. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

8. Shell Programming and Scripting

Date Pattern Match (replacement string)

Hello, i am splitting files and sometimes the string of the pattern doesnt exist in the input file it starts for example with 00:00:01. So the output is completely desorganized, is there any way of putting a replacement string in the pattern so it will grab all the times from 00:**:** to first... (0 Replies)
Discussion started by: x-plicit78
0 Replies

9. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

10. Shell Programming and Scripting

replace a column in a file if it matches certain pattern

Hi, I want to replace a column in a file if it matches certain pattern. Can you help me on this. Here is the file content. 000000 1111111 2222222 011111 0123445 1234556 023445 1111111 2343455 if second column contains 1111111 i need to replace it with 0000000 Can you... (6 Replies)
Discussion started by: Krrishv
6 Replies
Login or Register to Ask a Question