Replace a line with a separate line in code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a line with a separate line in code
# 1  
Old 02-19-2010
Replace a line with a separate line in code

I have a bunch of files that are like this:

Code:
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Header</h1>
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Unique</h1>
<html stuffs>HTML STUFFS</endhtmlstuffs>

And Here's what I'd like it to look like:

<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Unique Header</h1>
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<htmlstuffs>HTML STUFFS</endhtmlstuffs>

I generally use perl for find and replace stuff, but I don't know how to get it to look for the next instance and replace it.

Any help would be greatly appreciated. Sorry if this is posted elsewhere. I did search first, but couldn't find an answer. Thanks
# 2  
Old 02-19-2010
Hello, kason:

Code:
$ cat data
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Header</h1>
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Unique</h1>
<html stuffs>HTML STUFFS</endhtmlstuffs>

$ awk -f kason.awk data
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<h1>Unique Header</h1>
<htmlstuffs>HTML STUFFS</endhtmlstuffs>
<html stuffs>HTML STUFFS</endhtmlstuffs>

$ cat kason.awk 
BEGIN { FS="<[^>]*>"; }

/^<h1>/ {
    if (buffering) {
        print "<h1>"$2" "h"</h1>";
        h="";
        if (buf) {
            printf("%s", buf);
            buf="";
        }
        buffering=0;
    }
    else {
        h=$2;
        buffering=1;
    }
    next;
}

buffering {
    buf=buf $0 RS;
    next;
}

{ print; }

Regards,
Alister

Last edited by alister; 02-19-2010 at 09:33 PM..
# 3  
Old 02-27-2010
Try this in Perl

Code:
use strict;
use warnings;


open(FH,"file") || die "Can't open the file";
my @line_arr;
my $check=0;
my $i=0;

while(<FH>)
{
        my $line=$_;
        chomp($line);
        if($line=~/^<h1>(.*)<\/h1>$/)
        {
                $line_arr[$i++]=$1;
        }
}
my @val=reverse @line_arr;
seek(FH,0,0);

while(<FH>)
{
        if(/^(<h1>)(.*)(<\/h1>)$/)
        {
                if($check==0)
                {
                        print $1."@val".$3."\n";
                        $check=1;
                }
        }
        else
        {
                print $_;
        }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

4. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

6. Shell Programming and Scripting

How to separate one line to mutiple line based on one char?

Hi Gurus, I need separate one file which is one huge line to mutiple line. file like abcd # bcd # def # fge # ged I want to get abcd bcd def fge ged Thanks in advance (4 Replies)
Discussion started by: ken6503
4 Replies

7. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

8. Shell Programming and Scripting

Read each line and saving the line in separate files

Hi Experts, I am having a requirement like this; Input file EIM_ACCT.ifb|1001|1005 EIM_ADDR.ifb|1002|1004 EIM_ABD.ifb|1009|1007 I want to read each line of this file and pass each line,one at a time,as an argument to another script. eg; 1.read first line->store it to a file->call... (2 Replies)
Discussion started by: ashishpanchal85
2 Replies

9. Shell Programming and Scripting

[Solved] making each word of a line to a separate line

Hi, I have a line which has n number of words with separated by space. I wanted to make each word as a separate line. for example, i have a file that has line like i am a good boy i want the output like, i am a good (8 Replies)
Discussion started by: rbalaj16
8 Replies

10. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies
Login or Register to Ask a Question