Visit Our UNIX and Linux User Community


Help to decode in perl script


 
Thread Tools Search this Thread
Top Forums Programming Help to decode in perl script
# 1  
Old 08-19-2009
Help to decode in perl script

Hi,
I am having a file in below stucture:

header
{
subheader1
{
field1 : value
field2 : value
field3: value
} //end of subheader1
subheader2
{
subheader3
{
field4 : value
field5 : value
field6: value
}
subheader4
{
field7 : value
field8 : value
field9: value
}
} // end of subheader2
}//end of header
header
{
...
...
} // having the same format
I need to convert this file into this format:
fileld1,field2,field3,field4,field5,field6,field7,field8,field9
value,value,value,value,value,value,value,value,value
....
....

I have tried with logic in below:
Can anybody tel me what is the best logic to decode it in Perl.
# 2  
Old 08-19-2009
Just as an idea, in case there isn't already a module on CPAN for this format, I'd convert it to YAML (easy, since it's already structured) and then iterate over the resulting structure.

Code:
# Warning: pseudo-code
foreach $line (<$file>)
    if $line eq '{'
        $indent++
        next
    if $line eq '}'
        $indent--
        next
    $yaml .= $indent.$line

YAML::Parse($yaml)

# 3  
Old 08-19-2009
Quote:
Originally Posted by pludi
Just as an idea, in case there isn't already a module on CPAN for this format, I'd convert it to YAML (easy, since it's already structured) and then iterate over the resulting structure.

Code:
# Warning: pseudo-code
foreach $line (<$file>)
    if $line eq '{'
        $indent++
        next
    if $line eq '}'
        $indent--
        next
    $yaml .= $indent.$line

YAML::Parse($yaml)

Pludi,
Thanks a lot for your reply.
Actually I have tried in the same way.
But main problem is that if I am searching the header then I have to traverse from "{" to "}" until the next heder.

You had given the indent++, how can I increase the counter that I can fetch from one header to another.
# 4  
Old 08-21-2009
hi,
can anybody help me what should I do
# 5  
Old 08-22-2009
Code:
perl -ne 'BEGIN {$i=0} chomp;
          if(/{/) {$i++}
          elsif(/:/) {split/[: ]+/;push @x,$_[0];push @y,$_[1]}
          elsif(/}/) {$i--;
                      if($i == 0) {
                        printf("%s\n%s\n",join(",",@x),join(",",@y));
                        @x=(); @y=()
                      }}' file

tyler_durden
# 6  
Old 08-27-2009
Hi tyler,
Thanks a lot for your reply. It is working fine, but my requirement is something different.
test.txt file:
-------------
[inmis@inapp cdr_kallol]$ cat test.txt
header
{
subheader1
{
field1 : value
field2 : value
field3: value
}
subheader2
{
subheader3
{
field4 : value
field5 : value
field6: value
}
subheader4
{
field7 : value
field8 : value
field9: value
}
}
}
header
{
subheader1
{
field1 : value1
field2 : value1
field3: value1
}
subheader2
{
subheader3
{
field4 : value1
field5 : value1
field6: value1
}
subheader4
{
field7 : value1
field8 : value1
field9: value1
}
}
}


[inmis@inapp cdr_kallol]$ perl -ne 'BEGIN {$i=0} chomp;
if(/{/) {$i++}
elsif(/:/) {split/[: ]+/;push @x,$_[0];push @y,$_[1]}
elsif(/}/) {$i--;
if($i == 0) {
printf("%s\n%s\n",join(",",@x),join(",",@y));
@x=(); @y=()
}}' test.txt
field1,field2,field3,field4,field5,field6,field7,field8,field9
value,value,value,value,value,value,value,value,value
field1,field2,field3,field4,field5,field6,field7,field8,field9
value1,value1,value1,value1,value1,value1,value1,value1,value1

But the requirement is :
field1,field2,field3,field4,field5,field6,field7,field8,field9
value,value,value,value,value,value,value,value,value
value1,value1,value1,value1,value1,value1,value1,value1,value1
.......

There may be n numbers of data and I require in this format.

I have tried some different way..
Can you please look into it?
use IO::File;
open (INPUT_FILE, $ARGV[0]) || die "cant open file\n";
my @body = <INPUT_FILE>;
my $count= 0;
$no_of_lines=scalar(@body);

for ( $i = 0 ; $i < $no_of_lines ; $i++)
{
$variable = $body[$i];

if ( $variable =~ "field1")
{

@tmp_var = split(/([:\'])/,$variable);
$field1 = $tmp_var[4];
print "$field1,";
}
if ( $variable =~ "field2")
{

@tmp_var = split(/([:\'])/,$variable);
$field2 = $tmp_var[4];
print "$field2,";
}
if ( $variable =~ "field2")
{

@tmp_var = split(/([:\'])/,$variable);
$field2 = $tmp_var[4];
print "$field2,";
}
...
...
}
print "\n";

It is a very bad way I have written, but main problem is that I can not write any else in the loop. I have to track whether any field1 or field2 is missing.

The logic is :

1. Match -> field1:value1
2. If field1 exists then print value1
3. else
4. print null

Then the output will come

field1,field2,field3,field4,field5,field6,field7,field8,field9
value,value,value,,value,value,value,value,value
value1,value1,value1,value1,value1,value1,,value1,value1

In the above output first line field4 is missing
and in the second line field7 is missing


Can you please help here ? ....

Regards

---------- Post updated at 06:56 AM ---------- Previous update was at 06:52 AM ----------

In the Code I have written

$field1 = $tmp_var[4];

actually original data written in this format

field1 : 'value1'

One single cote is there
# 7  
Old 08-29-2009
Can you help me whether it is possible in awk ?

Previous Thread | Next Thread
Test Your Knowledge in Computers #19
Difficulty: Easy
Vint Cerf is generally referred to as the 'Father of the Internet'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decode a file

hi i have this file : <?xml version="1.0" encoding="UTF-8"?> <OnDemand xmlns="http://xsd.telecomitalia.it/Schema/crmws.entity.OnDemand" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xsd.telecomitalia.it/Schema/crmws.entity.OnDemand... (2 Replies)
Discussion started by: Francesco_IT
2 Replies

2. UNIX for Dummies Questions & Answers

What does this command do? and how to decode all other lines

Hello, I have an encoded file and I wish to see what is written inside. The first line is given below: eval "$(dd if=$0 bs=1 skip=69 2>/dev/null|gpg -d 2>/dev/null)"; exit PS: When I google above code, it says that command is use for encryption... Following lines include many strange... (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

How to decode text files?

HI experts....I am trying to decode some text files... I need little help from you people...file is of mixed type...from generated from windows based system my text files looks like this... 2.AUBZ 158 1 11 116204310 6 N 7542 E 18 02846 52833 102821 152815 ... (9 Replies)
Discussion started by: Akshay Hegde
9 Replies

4. AIX

How to decompile or decode an executable?

I have a file type of: executable (RISC System/6000 V3.1) or obj module not stripped How can I decode it? I know that this may be somewhat involved, but if you could steer me in the right direction, I would appreciate it. Thanks! (2 Replies)
Discussion started by: gg48gg
2 Replies

5. Linux

Decode the statement!!

What will the below statement do ?:confused: && { && {eval `/bin/setup 1`} || && { VAR="/tmp" } export $VAR; } (3 Replies)
Discussion started by: krishnaux
3 Replies

6. Programming

How to Decode an image using openGL

Hi, How to decode an image using openGL library libjpeg .. which are the steps needed to do this using C language.. actually my work is to decode the image, store it on the buffer, and place it on cube surface.. please guide me,,any answer will appreciated .. (8 Replies)
Discussion started by: Ravikishore
8 Replies

7. Shell Programming and Scripting

auto decode a value to different value

Okay, This is not something I've tried to do before, but what I want (need) to do is when a value is read in it gets changed to the value needed. I've been given a list of ids that I need to check against the ids I have in my system, but as is the case we don't have the same naming convention... (2 Replies)
Discussion started by: nhatch
2 Replies

8. Shell Programming and Scripting

Decode email

In pine email, when you receive an email with an attachment, the content of the attachment is incoded (content-transfer-encoding: base64). I am trying to write a shell script that will read the mail file, and it will save the attachment of an email to a directory. I want to do this by reading... (6 Replies)
Discussion started by: mskarica
6 Replies

9. Programming

gunzip and base64 decode a string

I am writing a C program to get messages from a JMS queue into a string variable and then write them to a database. The messages are compressed (gzip) and encoded (base64), so I need to be able to perform gunzip and base64 decode inside the C. I don't want to write any of the messages to file so... (2 Replies)
Discussion started by: handak9
2 Replies

10. Shell Programming and Scripting

Please decode in English

Hello: Can anyone please decode this script in English. I have also made some comments which I know.. The actual script does not have one comment also.. #! /bin/ksh . odbmsprd_env.ksh #setting the env.. echo $0 Started at : `date '+%d-%m-%Y %H:%M:%S'` # what's echo $0 ... (4 Replies)
Discussion started by: ST2000
4 Replies

Featured Tech Videos