Unix Shell - creating arrays from text files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > creating arrays from text files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author creating arrays from text files
Shell Solution

2007-12-26, 1:25 pm

Hi,
I have a file that looks like this:

<MOID>
1
2
2
</MOID>
<MOID>
3
4
5
</MOID>
<MOID>
4
5
6
</MOID>


----------------------------------------------------------------------------------------

And I want to convert it to this:

moid1,1
moid1,2
moid1,2

moid2,3
moid2,4
moid2,5

moid3,4
moid3,5
moid3,6


Please advise.
Janis Papanagnou

2007-12-26, 1:25 pm

Shell Solution wrote:
> Hi,
> I have a file that looks like this:
>
> <MOID>
> 1
> 2
> 2
> </MOID>
> <MOID>
> 3
> 4
> 5
> </MOID>
> <MOID>
> 4
> 5
> 6
> </MOID>
>
>
> ----------------------------------------------------------------------------------------
>
> And I want to convert it to this:
>
> moid1,1
> moid1,2
> moid1,2
>
> moid2,3
> moid2,4
> moid2,5
>
> moid3,4
> moid3,5
> moid3,6
>
>
> Please advise.



awk '/^<M/{m++}/[0-9]/{print "moid"m ","$1}/^<\//{print ""}' your_file


Janis
mik3l3374@gmail.com

2007-12-27, 1:37 am

On Dec 26, 10:55 pm, Shell Solution <esoimeme.geo...@gmail.com> wrote:
> Hi,
> I have a file that looks like this:
>
> <MOID>
> 1
> 2
> 2
> </MOID>
> <MOID>
> 3
> 4
> 5
> </MOID>
> <MOID>
> 4
> 5
> 6
> </MOID>
>
> ----------------------------------------------------------------------------------------
>
> And I want to convert it to this:
>
> moid1,1
> moid1,2
> moid1,2
>
> moid2,3
> moid2,4
> moid2,5
>
> moid3,4
> moid3,5
> moid3,6
>
> Please advise.


if you have GNU awk

awk 'BEGIN{RS="</MOID>\n<MOID>|<MOID>|</MOID>"}
{
for ( i=1 ; i<=NF;i++) print "moid"m,$i
m++
}' file
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com