Replace Text Based On Pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace Text Based On Pattern
# 1  
Old 10-14-2009
Replace Text Based On Pattern

Hi I'm trying to replace text in a file based upon a pattern.

The pattern I'm looking for is:

Code:
<styleURL>#style0002</styleURL>
<name>#######6105#######</name>

The # are seven alphanumeric characters before and after 6105.

I need it to replace that with this recursively:

Code:
<styleURL>#style0003</styleURL>
<name>#######6105#######</name>

Any ideas would be appreciated.

Thanks
# 2  
Old 10-15-2009
If the two lines are consecutive in your file you can try something like this:

Code:
awk '
/\<styleURL\>#style0002\<\/styleURL\>/ {
  print "<styleURL>#style0003</styleURL>"
  print "<name>#######6109#######</name>"
  getline;next
}1' file

# 3  
Old 10-18-2009
That will replace

Code:
<styleURL>#style0002</styleURL>
<name>*******6105*******</name>

with

Code:
<styleURL>#style0003</styleURL>
<name>*******6105*******</name>

?

Just double checking.

I was wrong when I used # to represent alphanumeric characters as the # before style0002 isnt an alphanumeric it is always there.

And yes they are always consecutive.

Let me know if you need to know anything else.

Thanks!
# 4  
Old 10-18-2009
clarification

Quote:
Originally Posted by Grizzly
That will replace

Code:
<styleURL>#style0002</styleURL>
<name>*******6105*******</name>

with

Code:
<styleURL>#style0003</styleURL>
<name>*******6105*******</name>

?

Just double checking.
It's the right idea - at least, it's how I would go about it - however, I think Franklin52 made a few typos, though, and one mistake.

First, s/6109/6105/; then, un-escape '<' and '>' ('\<' and '\>' have a meaning of their own; they match the empty string at the begging, end of a word). I would lose the '1' after the closing '}' as well; I think that's a typo (if I'm wrong and it does serve a purpose, someone please correct me...).

Keep in mind that the code as presented below doesn't need an explicit 'next' statement - you can include one, but you don't need to in this instance (it's implicit). If you ever have a need to skip to the next record immediately after doing something, though, use next; it's analogous to continue in a for loop.

Fix those minor issues and I think that code will do what you want it to do; that is, assuming that I'm understand correctly what you're trying to do. Restated, that code is:

Code:
awk '/<styleURL>#style0002<\/styleURL>/ {
   print "<styleURL>#style0003</styleURL>";
   print "<name>#######6105#######</name>";
   getline;
}'

Personally, I love awk for tasks of this nature.

- Larry

Last edited by Time2IPL; 10-18-2009 at 11:22 AM.. Reason: fix code tags
# 5  
Old 10-18-2009
You just want to replace "style0002" with "style0003" within the styleURL tag and the 4 numbers after the 7th position of the name tag, right?

Try something like this, I've used awk variables for the values, adjust them for your own fit:

Code:
awk -v old="style0002" -v new="style0003" -v num="1234" '
/\<styleURL\>/ && $0 ~ old {
  sub(old, new)
  print
  getline
  print substr($0, 1,13) num substr($0, 18)
  next
}1' file

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 in text

hi unix expert is there any command in linux to repace a pattern in the text to another pattern? many thanks samad (2 Replies)
Discussion started by: abdossamad2003
2 Replies

2. Shell Programming and Scripting

Grab text after pattern and replace

i have a file which contains data seperated by comma. i want to replace text after 3rd occurrence of a comma. the input file looks like this abcdef,11/02/2015 11:55:47,1001,1234567812345678,12364,,abc abcdefg,11/02/2015 11:55:47,01,1234567812345678,123,,abc abcdefhih,11/02/2015... (4 Replies)
Discussion started by: gpk_newbie
4 Replies

3. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

4. Shell Programming and Scripting

Search and Replace a text if the line contains a pattern

My text file looks like below . . . abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt $Bad_abcd_REJ=rcrd_op.txt ghijklm $Bad_abcd_TYHS=rcrd_op.txt abcgd abcdefghi jklmnop $Bad_ptrq_GTS=rcrd_ip.txt (2 Replies)
Discussion started by: machomaddy
2 Replies

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

6. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

7. Shell Programming and Scripting

find pattern and replace the text before it

i am editing a big log file with the following pattern: Date: xxxx Updated: name Some log file text here Date: eee Updated: ny Some log file text here Basically i want to remove all the text in a line before the "Updated" pattern. I sill want to print the other... (4 Replies)
Discussion started by: balan1983a
4 Replies

8. Shell Programming and Scripting

Replace text based on column

Replace these if the column is - p = q q = p r = s s = r input - PQRSSP + PQRS output - QPSRRQ + PQRS Some thing,like (5 Replies)
Discussion started by: bumblebee_2010
5 Replies

9. Shell Programming and Scripting

Merge lines in text file based on pattern

Hello, I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples.... I should say, I am very inexperienced with text processing. I have a text file with approx 60k lines in it. I need to merge lines based on the number... (8 Replies)
Discussion started by: Bertik
8 Replies

10. AIX

Pattern to replace ^M and ^Y in a 4.2 AIX text file

I have files on my AIX 4.2 client system where I need to do the following replacements below but have no clue how ? They are control characters (linefeed, chariage return, ...). First, replace "^M^Y^M" with ^char_for_end_of_line Then replace "^M" with " " Trim all left spaces In VI, my... (7 Replies)
Discussion started by: Browser_ice
7 Replies
Login or Register to Ask a Question