Help with replace the content of specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with replace the content of specific pattern
# 1  
Old 12-14-2011
Help with replace the content of specific pattern

Input file:
Code:
__<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:
Code:
__<tmp>AWEETET</tmp>
____<name_evidence="3"_type="2@#">QEWQE</name>
__<tmp>QWE048</tmp>
____<name_evidence="3"_type="570">@#@$#545</name>
____<name_evidence="2"_type="351">QWE4</name>

Command that I tried:
Code:
[home@perl_beginner]sed 's/^__<name>[a-z]*[0-9]*<\/name>/^__<tmp>[a-z]*[0-9]*<\/tmp>/g' input_file.txt

Thanks for any advice.
# 2  
Old 12-14-2011
In your replacement you cannot use expressions such as .* or [0-9]. You need to put parentheses round the portion(s) of the pattern that you wish to "copy into the replacement string when the pattern is matched. Back references (\1 in this case) are used in the replacement to show where the contents of the portion of the string that matched inside of the parentheses are to be copied.

Given that, this might work:
Code:
sed  's/^__<name>\(.*\)<\/name>/__<tmp>\1<\/tmp>/' data-file

# 3  
Old 12-15-2011
Quote:
Originally Posted by perl_beginner
Command that I tried:
Code:
[home@perl_beginner]sed 's/^__<name>[a-z]*[0-9]*<\/name>/^__<tmp>[a-z]*[0-9]*<\/tmp>/g' input_file.txt

Additionally, words following <name> are in UPPER case, the above highlighted would match the lower cases only and [a-z]*[0-9]* sequence tells that zero or more occurrences of alphabets followed by zero or more occurrences of numeric, which may sometimes fail if the word is an alpha-numeric as in QWE048TRR. This could be rectified by giving the regex as [A-Z0-9]*
Code:
sed 's/__<name>\([A-Z0-9]*\)<\/name>/__<tmp>\1<\/tmp>/' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove content present in between specific pattern ?

Hi, I have a file with following pattern. We are looking to filter out only specific content from this file. sample BLAdmins Server.* LinuxAdmins Server.* Policy Name: Recommended Default ACL Policy Everyone ACLPushJob.Read Everyone ACLTemplate.Read Everyone ... (9 Replies)
Discussion started by: Litu19
9 Replies

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

3. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

4. Shell Programming and Scripting

Replace specific characters until pattern

Hi experts, My file looks something like this. 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? abcXX4,7,234 abc_defg_45XX23,74,123 Thanks! (5 Replies)
Discussion started by: abercrom
5 Replies

5. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

6. Shell Programming and Scripting

Need help to replace a pattern on specific line in a data file

Hi, I want to replace specific pattern "-2.0000 2" by "1.0000 3" on a particular line (line #5) in a file 1.dat. I have about 50 more files similar to 1.dat in which I want to do this correction. Can you please suggest how can I make change only at this particular line of a file... (6 Replies)
Discussion started by: anuj06
6 Replies

7. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

9. Shell Programming and Scripting

Remove specific pattern header and its content problem facing

Input file: >TRACK: Position: 1 TYPE: 1 Pos: SVAVPQRHHPGGTVFREPIIIPAIPRLVPGWNKPIIIGRHAFGDQYRATDRVIPGPGKLE LVYTPVNGEPETVKVYDFQGGGIAQTQYNTDESIRGFAHASFQMALLKGLPLYMSTKNTI LKRYDGRFKDIFQEIYESTYQKDFEAKNLWYEHRLIDDMVAQMIKSEGGFVMALKNYDGD >TRACK: Position: 1 TYPE: 2 Pos: FAHASFQMALLKGLPLYMS... (8 Replies)
Discussion started by: patrick87
8 Replies

10. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question