please help... format change and vice-versa.... 
Author Message
 please help... format change and vice-versa....

hallo,

I need your help.  Please.....  (This one is really difficult for me.)

How can I change the following:

MIC-5467846329 2000-02-16 00:10:30 loc ME-1 M111111.222222 47AVS/ere2
NAME:1     "Jennifer Is MyName"
TOTO=32323                         KJ=fbfd
HEX1=0x0002                        HEX3=0xffff
HEX2=0xc1                          ZOO=232
NONO=2                             DEW=213
MONO=6

MIC-6484356935 2000-02-16 00:10:30 vyf TR-5 C131467.347232 523S24S/gr42
NAME:2     "Jen Is MyName"
TOTO=72454                         KJ=aace
HEX1=0x0046                        HEX3=0xfeff
HEX2=0xfa                          ZOO=853
NONO=23                            DEW=34
MONO=6

.
.
.
.

to this (made it pip delimited).

MIC-5467846329|2000-02-16|00:10:30|loc|ME-1|M111111.222222|47AVS/ere2|1|
"Jennifer Is MyName"|32323|0x0002|0xc1|2|6|fbfd|0xffff|232|213
MIC-6484356935|2000-02-16|00:10:30|vyf|TR-5|C131467.347232|523S24S/gr42|
1|"Jen Is MyName"|72454|0x0046|0xfa|23|6|aace|0xfeff|853|34
.
.
.

and how can I make it back to original format.

I think i nid two awk scripts for the two conversions.  GNUawk 3.04. and
mawk is available for me.

Thenk you.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 04 Aug 2002 03:00:00 GMT  
 please help... format change and vice-versa....
Hi,

Here is how I would do it, unfortunately for you just typed in and untested.

It only covers the from-to converstion, and not the conversion back. I
recommend you
seperate it in two different and manageble scripts. My example could be much

shorter/fancier, but I think this is a very straightforward solution.

First I assume each piece of data is seperated by a blank line, you didn't
mention this. The following code keeps a counter which increases at each
line.
For each value of the counter I have special actions because the format
changes
from line to line. For each line I append the data in one long string.
When the counter changes back to 0, or at the end of the program,
I print the long string.

The way it's done now makes it easy to add per-line validation checks, if
required.

A note on the conversion back: start the script with:
BEGIN { FS = "|" }
Now you get all your fields nicely seperated in $1 $2 etc. Easy to make a
script that
writes each line in the original multiline format. (Assuming it is one line,
and not
two like in your posting which I think is from linewrapping.

Success,
Erik Janssen.

/^$/  { line_count = 1;
        if (data_present == 1)
        {
           print long_string
           data_present = 0
        }
        next       # Stop here and continue with next line
      }

line_count == 1  { data_present = 1
                   long_string = $1 "|" $2 "|" $3 "|" $4 "|" $5 "|" $6 "|"
$7
                   line_count++
                 }

line_count == 2  {  # First remove everything up to the number from $1
                    sub( /^.*:/,"", $1 )
                     # I assume your name is always three positions. If not
add a function
                     # that returns a string containing all fields starting
from x and put
                     # your_function(2) in place of $2 $3 $4

                    long_string = long_string "|" $1 "|" $2 $3 $4
                    line_count++
                  }

line_count == 3   { # Remove garbage from fields (from start of field up to
'=', including)
                    sub( /^.*=/,"",$1 )
                    sub( /^.*=/,"",$2 )
                    long_string = long_string "|" $1 "|" $2
                    line_count++
                   }

line_count == 4    { ...As with 3 }

line_count == 5    { ...As with 3.... }

line_count == 6    { ....As with 3... }

line_count == 7    { ... Your example leaves it out....

END { do the same here as with the empty line }

Quote:

> hallo,

> I need your help.  Please.....  (This one is really difficult for me.)

> How can I change the following:

> MIC-5467846329 2000-02-16 00:10:30 loc ME-1 M111111.222222 47AVS/ere2
> NAME:1     "Jennifer Is MyName"
> TOTO=32323                         KJ=fbfd
> HEX1=0x0002                        HEX3=0xffff
> HEX2=0xc1                          ZOO=232
> NONO=2                             DEW=213
> MONO=6

> MIC-6484356935 2000-02-16 00:10:30 vyf TR-5 C131467.347232 523S24S/gr42
> NAME:2     "Jen Is MyName"
> TOTO=72454                         KJ=aace
> HEX1=0x0046                        HEX3=0xfeff
> HEX2=0xfa                          ZOO=853
> NONO=23                            DEW=34
> MONO=6

> .
> .
> .
> .

> to this (made it pip delimited).

> MIC-5467846329|2000-02-16|00:10:30|loc|ME-1|M111111.222222|47AVS/ere2|1|
> "Jennifer Is MyName"|32323|0x0002|0xc1|2|6|fbfd|0xffff|232|213
> MIC-6484356935|2000-02-16|00:10:30|vyf|TR-5|C131467.347232|523S24S/gr42|
> 1|"Jen Is MyName"|72454|0x0046|0xfa|23|6|aace|0xfeff|853|34
> .
> .
> .

> and how can I make it back to original format.

> I think i nid two awk scripts for the two conversions.  GNUawk 3.04. and
> mawk is available for me.

> Thenk you.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 09 Aug 2002 03:00:00 GMT  
 please help... format change and vice-versa....
Mmmm, I overlooked the complication that you want the fields out of
order. I try to get other work done too here, that gives problems :-)
What you need to do:

in each action after "line_count == x", do not build up the string directly
but store evey item in a seperate variable. Then, when the line is to be
printed
build up the string from all those variables.

Success again,
Erik Janssen



Fri, 09 Aug 2002 03:00:00 GMT  
 please help... format change and vice-versa....

Quote:

> [snip]

> How can I change the following:

> [snip]

> to this (made it pip delimited).

> MIC-5467846329|2000-02-16|00:10:30|loc|ME-1|M111111.222222|47AVS/ere2|1|
> "Jennifer Is MyName"|32323|0x0002|0xc1|2|6|fbfd|0xffff|232|213
> MIC-6484356935|2000-02-16|00:10:30|vyf|TR-5|C131467.347232|523S24S/gr42|
> 1|"Jen Is MyName"|72454|0x0046|0xfa|23|6|aace|0xfeff|853|34

> [snip]

> and how can I make it back to original format.

> I think i nid two awk scripts for the two conversions. GNUawk 3.04. and
> mawk is available for me.

Perl is better than awk for this kind of text parsing task:

$ cat jenny.pl
#!/usr/bin/perl -w

use strict;

$/ = '';   # paragraph mode
$\ = "\n";

while (<>) {

                     (\S+)\s+(\S+)\s+(\S+)\s+    #   3,  4,  5,
                     (\S+)\s+                    #   6,
                     NAME:(\S+)\s+"([^"]*)"\s+   #   7,  8,
                     TOTO=(\S+)\s+KJ=(\S+)\s+    #   9, 10,
                     HEX1=(\S+)\s+HEX3=(\S+)\s+  #  11, 12,
                     HEX2=(\S+)\s+ZOO=(\S+)\s+   #  13, 14,
                     NONO=(\S+)\s+DEW=(\S+)\s+   #  15, 16,
                     MONO=(\S+)                  #  17
                 }x;


Quote:
}

__END__
$ cat jenny.txt
MIC-5467846329 2000-02-16 00:10:30 loc ME-1 M111111.222222 47AVS/ere2
NAME:1     "Jennifer Is MyName"
TOTO=32323                         KJ=fbfd
HEX1=0x0002                        HEX3=0xffff
HEX2=0xc1                          ZOO=232
NONO=2                             DEW=213
MONO=6

MIC-6484356935 2000-02-16 00:10:30 vyf TR-5 C131467.347232 523S24S/gr42
NAME:2     "Jen Is MyName"
TOTO=72454                         KJ=aace
HEX1=0x0046                        HEX3=0xfeff
HEX2=0xfa                          ZOO=853
NONO=23                            DEW=34
MONO=6

$ perl jenny.pl jenny.txt
MIC-5467846329|2000-02-16|00:10:30|loc|ME-1|M111111.222222|47AVS/ere2|
1|Jennifer Is MyName|32323|0x0002|0xc1|2|6|fbfd|0xffff|232|213
MIC-6484356935|2000-02-16|00:10:30|vyf|TR-5|C131467.347232|523S24S/gr42|
2|Jen Is MyName|72454|0x0046|0xfa|23|6|aace|0xfeff|853|34
$

Modify the regular expression pattern to taste.

Reversing this transformation is equally trivial in Perl and is
left as an exercise.

HTH.

--
Jim Monty

Tempe, Arizona USA



Sat, 10 Aug 2002 03:00:00 GMT  
 please help... format change and vice-versa....

Quote:

> #!/usr/bin/perl -w

> use strict;

> $/ = '';   # paragraph mode
> $\ = "\n";

> while (<>) {

>                      (\S+) \s+ (\S+) \s+              #   2,  3,
>                      (\S+) \s+ (\S+) \s+              #   4,  5,
>                      (\S+) \s+                        #   6,
>                      NAME: (\S+) \s+ "([^"]*)"   \s+  #   7,  8,
>                      TOTO= (\S+) \s+ KJ=   (\S+) \s+  #   9, 10,
>                      HEX1= (\S+) \s+ HEX3= (\S+) \s+  #  11, 12,
>                      HEX2= (\S+) \s+ ZOO=  (\S+) \s+  #  13, 14,
>                      NONO= (\S+) \s+ DEW=  (\S+) \s+  #  15, 16,
>                      MONO= (\S+)                      #  17
>                  /x;


> }

> __END__

This reduces nicely to a simple one-liner. ;-)

$ cat jenny.txt
MIC-5467846329 2000-02-16 00:10:30 loc ME-1 M111111.222222 47AVS/ere2
NAME:1     "Jennifer Is MyName"
TOTO=32323                         KJ=fbfd
HEX1=0x0002                        HEX3=0xffff
HEX2=0xc1                          ZOO=232
NONO=2                             DEW=213
MONO=6

MIC-6484356935 2000-02-16 00:10:30 vyf TR-5 C131467.347232 523S24S/gr42
NAME:2     "Jen Is MyName"
TOTO=72454                         KJ=aace
HEX1=0x0046                        HEX3=0xfeff
HEX2=0xfa                          ZOO=853
NONO=23                            DEW=34
MONO=6

$ perl -00 -l012 -wpe '$_ = join "|", (/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)
\s+(\S+)\s+(\S+)\s+(\S+)\s+NAME:(\S+)\s+"([^"]*)"\s+TOTO=(\S+)\s+KJ=(\S+)
\s+HEX1=(\S+)\s+HEX3=(\S+)\s+HEX2=(\S+)\s+ZOO=(\S+)\s+NONO=(\S+)\s+DEW=
(\S+)\s+MONO=(\S+)/)[0..9,11,13,15,17,10,12,14,16]' jenny.txt
MIC-5467846329|2000-02-16|00:10:30|loc|ME-1|M111111.222222|47AVS/ere2|
1|Jennifer Is MyName|32323|0x0002|0xc1|2|6|fbfd|0xffff|232|213
MIC-6484356935|2000-02-16|00:10:30|vyf|TR-5|C131467.347232|523S24S/gr42|
2|Jen Is MyName|72454|0x0046|0xfa|23|6|aace|0xfeff|853|34
$

Look, Ma! No user-defined variables!

--
Jim Monty

Tempe, Arizona USA



Sat, 10 Aug 2002 03:00:00 GMT  
 please help... format change and vice-versa....
Charles, Jim and Eric,

Thanks a lot for your help.

jen

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 23 Aug 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Converting Source Code to ASCII Format (and vice versa)

2. (correction)please help... format change and vice-versa....

3. printf: dec to hex (vice-versa)

4. C4b - 16-bit To 32-bit and Vice Versa

5. Converting binary to decimal and vice versa?

6. dbf to xls and vice-versa

7. ANSI to OEM and vice versa

8. where can i find examples of accesing labview from matlab or vice-versa

9. From INTEGER to BIT and vice versa

10. Rexx to CLIST and Vice Versa

11. Arithmetical operations on a string (& vice versa)

12. Arithmetical operations on a string (& vice versa)

 

 
Powered by phpBB® Forum Software