Text replacement with awk or sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text replacement with awk or sed?
# 1  
Old 08-13-2015
Text replacement with awk or sed?

Hi guys,

I worked for almost a half-day for the replacement of some text automatically with script. But no success.
The problem is I have hundred of files, which need to be replaced with some new text. It's a painful work to work manually and it's so easy to do it wrong.

For example,

I have a main text as follows,
Code:
 <molecule id="a_feco4_s">
...many lines...
   <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">37.95 67.28 74.69 90.71 91.25 345.01 355.62 366.66 422.13 439.40 451.58 467.53 486.57 533.13 584.49 637.10 642.25 2009.41 2032.85 2036.21 2122.57 </array>
   </property>
...many lines...
 </molecule>
 <molecule id="c_feco4_t">
...many lines...
   <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">58.43 72.58 75.35 80.62 87.16 318.76 324.15 324.42 375.24 419.54 444.45 447.44 488.53 505.86 517.68 538.85 543.24 2029.98 2035.26 2040.33 2111.69 </array>
   </property>
...many lines...
 </molecule>
 <molecule id="b_feco4_t-3">
...many lines...
  <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">49.30 75.79 77.16 79.22 84.50 310.74 316.63 342.47 399.25 453.74 464.46 465.14 508.24 521.78 550.22 569.23 572.50 2010.41 2033.03 2034.16 2103.89 </array>
   </property>
...many lines...
 </molecule>

The text need to be replaced is 'all the numbers' inside (<array units="cm-1"> </array>). These new numbers are stored in the three separate files named 'a_feco4_s', 'c_feco4_t', and 'b_feco4_t-3' as shown below,
a_feco4_s
Code:
37.9521
90.7132
355.6175
439.3995
486.5668
637.1002
2032.8518
67.2767
91.2457
366.6612
451.5826
533.1265
642.2546
2036.2149
74.6883
345.0079
422.1317
467.5309
584.4914
2009.4132
2122.5729

c_feco4_t
Code:
58.4306
80.6210
324.1468
419.5376
488.5270
538.8506
2035.2576
72.5771
87.1616
324.4229
444.4529
505.8570
543.2370
2040.3282
75.3460
318.7635
375.2418
447.4447
517.6800
2029.9848
2111.6905

b_feco4_t-3
Code:
61.14
62.67
83.25
85.59
87.24
341.07
342.04
348.23
409.08
416.25
427.89
470.65
550.47
550.78
564.48
590.82
1893.71
2015.17
2032.38
2061.02

After the replacement, we will get
Code:
 <molecule id="a_feco4_s">
...many lines...
   <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">
37.9521
90.7132
355.6175
439.3995
486.5668
637.1002
2032.8518
67.2767
91.2457
366.6612
451.5826
533.1265
642.2546
2036.2149
74.6883
345.0079
422.1317
467.5309
584.4914
2009.4132
2122.5729
 </array>
   </property>
...many lines...
 </molecule>
 <molecule id="c_feco4_t">
...many lines...
   <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">
58.4306
80.6210
324.1468
419.5376
488.5270
538.8506
2035.2576
72.5771
87.1616
324.4229
444.4529
505.8570
543.2370
2040.3282
75.3460
318.7635
375.2418
447.4447
517.6800
2029.9848
2111.6905
 </array>
   </property>
...many lines...
 </molecule>
 <molecule id="b_feco4_t-3">
...many lines...
  <property title="Vibrational Frequencies" dictRef="me:vibFreqs">
    <array units="cm-1">
61.14
62.67
83.25
85.59
87.24
341.07
342.04
348.23
409.08
416.25
427.89
470.65
550.47
550.78
564.48
590.82
1893.71
2015.17
2032.38
2061.02
 </array>
   </property>
...many lines...
 </molecule>

I thank you very much for your kind help!

Zhen
# 2  
Old 08-13-2015
What operating system and shell are you using?

What have you tried to solve this problem?
# 3  
Old 08-13-2015
Code:
#! /usr/bin/perl

$/ = undef;
$_ = <>;

sub slurp { open FH, '<', @_ or die @_; return <FH>; }

s{(<molecule\s+id="([^"]*)".*?<array[^>]*>)(.*?)(</array.*?</molecule>)}
 {join $,, $1, slurp($2), $4}gemsx;

print;

This will replace the values in <array>...</array> with the contents of the file specified by the id attribute of <molecule id="name">.

Last edited by derekludwig; 08-15-2015 at 08:25 AM.. Reason: grammar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed or awk Replacement/Addition

Hi, I have a big text file with similar data as below and need the text as in output using awk or sed. any help is greatly appreciated. Input: City=Chicago Elden street >>> reservedBy = business 1 >>> reservedBy = business 2 >>> reservedBy = business 3 City=Dallas Elm street >>>... (5 Replies)
Discussion started by: tech_frk
5 Replies

2. Shell Programming and Scripting

sed text replacement

Hello, I'm using Bash and Sed to replace text within a text file (1.txt) twice in one script. Using a for loop I'm initially replacing any 'apple' words with the variable 'word1' ("leg). I'm then using another for loop to replace any 'apple' words with the variable 'word2' ("arm"). This task is... (2 Replies)
Discussion started by: Flip-Flop
2 Replies

3. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

4. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

5. Debian

Using awk and sed to replace text

Good Day Every one I have a problem finding and replacing text in some large files that will take a long time to manually edit. Example text file looks like this #Example Large Text File unix linux dos squid bind dance bike car plane What im trying to do is to edit all the... (4 Replies)
Discussion started by: linuxjunkie
4 Replies

6. Shell Programming and Scripting

Block of text replacement using sed

Hi, I have a requirement in which i need to replace text as below - <stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt; &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt; &lt;Param Name=&quot;Value&quot;... (0 Replies)
Discussion started by: abhitanshu
0 Replies

7. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

8. UNIX for Dummies Questions & Answers

Simple awk script for positional replacement in text?

I have a string of letters. (They happen to be DNA, not that it's relevant to the question.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position. I also need to ignore differences at other... (10 Replies)
Discussion started by: JFS
10 Replies

9. UNIX for Dummies Questions & Answers

Sed text replacement issue.

Hi, Im trying to find and replace text within a unix file using sed. The command that i have been using is sed '/,null,/ s//, ,/g' result.txt>result.tmp for replacing ",null," with ", ,". But this only replaces the first occurrance of ,null, in every line. I want to do it globally. It... (7 Replies)
Discussion started by: sohaibs
7 Replies

10. UNIX for Dummies Questions & Answers

Awk/Sed One liner for text replacement

Hi group, I want to replace the occurance of a particular text in a paragraph.I tried with Sed,but Sed only displays the result on the screen.How can i update the changes in the original file??? The solution should be a one liner using awk and sed. Thanks in advance. (5 Replies)
Discussion started by: bishnu.bhatta
5 Replies
Login or Register to Ask a Question