NEED HELP (AWK or anything that would work)

[LEFT]Hi guys, heres my first post.....

Input.txt:
<abc a=""
b=""
c=""
>
<error x=""
y=""
z=""
/>
</abc>

            &lt;abc a=""
			     e=""
			     c=""
			 &gt;
                &lt;error q=""
			           y=""
			           z=""
                /&gt;
            &lt;/abc&gt;

            &lt;abc
                ...
                ...
                ...
            &lt;/abc&gt;   

[/LEFT]

Requirement:

I need to cut out individual <abc> .... </abc> and redirect them to files.
In this case I have 3 <abc> .... </abc>, so they should go in 3 seperate files.

If I use awk '/<abc/,/<\/abc>/' , it selects from first <abc to last </abc>....

Hope some one can help me with this....

This should do the job:

awk '/<abc.*/{f=1};/<\/abc>/{f=0;print}f' file

Regards

GNU awk and assuming separated by blank lines

awk 'BEGIN{RS=""}/abc/{ print $0 > ++c".txt"}' file

@Franklin52

it didnt work :frowning:

[qzv2jm] <~>$ cat abc.txt
<abc a=""
b=""
c=""
>
<error x=""
y=""
z=""
/>
</abc>
<abc a=""
e=""
c=""
>
<error q=""
y=""
z=""
/>
</abc>

[qzv2jm] <~>$ awk '/<abc.*/{f=1};/<\/abc>/{f=0;print}f' abc.txt
awk: syntax error near line 1
awk: bailing out near line 1

@ghostdog74

It worked perfectly when I give a blank space, but saddly thats not the case :(, had given blank space in example only for giving u a better view..sry my mistake :D....

Oops, I forgot the seperate the files:eek:

Regards

awk 'BEGIN{RS="</abc>"}
/abc/{
 printf "%s%s" , $0 , "</abc>" > ++c".txt"
}' file

Another way:

awk '/<abc.*/{f=1;n++} 
/<\/abc>/{f=0;print $0 > n}
f{print $0 > n}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Hey your almost there :stuck_out_tongue: ....it is cuttin <abc> </abc> but its totall ignoring the tags inside it ie <error> in this case...is there a work around....

also can u please explain in short wat ur doing over here....i mean i understood 'awk 'BEGIN{RS="</abc>"} /abc/' and ++c".txt".... dint understand the printf part....:confused:

Thanks in advance

:b: WE HAVE A WINNER!!!! :b:

Thanks Franklin, really appretiate it.
Also thanks to ghostdog74.

You guys rock....GOD bless.

PS: can u direct me to some documents that would help me understand awk and sed....???

Also @Franklin, can you explain in short wat u just did...

i don't understand what you mean.
please try Franklin's code instead.

Sure.

awk '/<abc.*/{f=1;n++} 
/<\/abc>/{f=0;print $0 > n}
f{print $0 > n}' file

/<abc.*/{f=1;n++} -> If the line contains the expression, set the flag f and change the name of the file (n)

/<\/abc>/{f=0;print $0 > n} -> If the line contains the expression, unset the flag f and print the last line to file n

f{print $0 > n} -> Print the line if the flag is set to the file n

This is a good document to start with (buy it!):

sed & awk

Regards