qzv2jm
February 29, 2008, 6:35am
1
[LEFT]Hi guys, heres my first post.....
Input.txt:
<abc a=""
b=""
c=""
>
<error x=""
y=""
z=""
/>
</abc>
<abc a=""
e=""
c=""
>
<error q=""
y=""
z=""
/>
</abc>
<abc
...
...
...
</abc>
[/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
qzv2jm
February 29, 2008, 7:19am
4
@Franklin52
it didnt work
[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
qzv2jm
February 29, 2008, 8:25am
8
Hey your almost there ....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....
Thanks in advance
qzv2jm
February 29, 2008, 8:36am
9
WE HAVE A WINNER!!!!
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.
qzv2jm:
WE HAVE A WINNER!!!!
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...
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