Link to home
Start Free TrialLog in
Avatar of richsark
richsarkFlag for United States of America

asked on

fix a txt file using perl or awk etc....

Hi Folks,

I require your help please.

I have a named.conf file that needs to be slightly reformatted.

The file has lots of comments on the first 40 or lines which is fine. But I need the important entries to be formatted slightly different.

The stuff looks like this: (( each entry on one line ))

zone "r1.fs.xx.us" in { type master; file "named.fwd.r1"; forwarders { }; allow-update { key ft.dhcp.ddns.key.; };};

zone "0.7.166.in-addr.arpa" in { type master; file "named.rev.0.7.166"; forwarders { }; allow-update { key ft.dhcp.ddns.key.; };};

I need it to be re-written to looks like so:

zone "r1.fs.xx.us" {
      type master;
file "named.fwd.r1";
forwarders { };
allow-update { key fs.dhcp.ddns.key.; };

};

zone "0.7.166.in-addr.arpa" {
      type master;
file "named.rev.0.7.166";
forwarders { };
allow-update { key fs.dhcp.ddns.key.; };
};

Would someone out there be so kind is to write some sort of perl script or shell script to make this happen please?

Thanks
Avatar of ozo
ozo
Flag of United States of America image

while( <DATA> ){
s/\{(.*)}/{\n   $1\n}/ && s/((?:\{[^}\n]*}|.)*?;\s)/$1\n/g;
print;
}
__DATA__
zone "r1.fs.xx.us" in { type master; file "named.fwd.r1"; forwarders { }; allow-update { key ft.dhcp.ddns.key.; };};

zone "0.7.166.in-addr.arpa" in { type master; file "named.rev.0.7.166"; forwarders { }; allow-update { key ft.dhcp.ddns.key.; };};
Avatar of richsark

ASKER

Hi ozo ,

Thanks for your response . Just to be on the same page. I should create a script named convert.pl in that file te contents will be:

while( <DATA> ){
s/\{(.*)}/{\n   $1\n}/ && s/((?:\{[^}\n]*}|.)*?;\s)/$1\n/g;
print;
}

To run this I would type:

Convert.pl input_file output_file > final.conf

Does that sound right?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right.. I caught that right after I submitted.

Will try it and report back.

Thanks ozo
Hi Ozo,
I ran it, but seem to get a format issue

$ ./convert.sh r1_named.conf

./convert.sh: line 1: syntax error near unexpected token `)'
./convert.sh: line 1: `while( <> ){'

Contents are:

while ( <> ){
s/\{(.*)}/{\n   $1\n}/ && s/((?:\{[^}\n]*}|.)*?;\s)/$1\n/g;
print;
}
I got it

did on the command line

perl convert.sh r1_named.conf
Fantastic help !