Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting file output 2

Status
Not open for further replies.

blarneyme

MIS
Jun 22, 2009
160
US
/etc/exports:
Code:
#
#
#
/dir/path hostname1.my.domain(rw,sync,no_root_squash,fsid=0) \
          hostname2.my.domain(rw,sync,no_root_squash,fsid=0) \
          hostname3.my.domain(rw,sync,no_root_squash,fsid=0) \
          hostname4.my.domain(rw,sync,no_root_squash,fsid=0) 
/other/path hostname1.my.domain(rw,sync,no_root_squash,fsid=0) \
            hostname2.my.domain(rw,sync,no_root_squash,fsid=0)
What I'd like:
Code:
hostA,/dir/path,hostname1.my.domain,rw sync no_root_squash fsid=0
hostA,/dir/path,hostname2.my.domain,rw sync no_root_squash fsid=0
hostA,/dir/path,hostname3.my.domain,rw sync no_root_squash fsid=0
hostA,/dir/path,hostname4.my.domain,rw sync no_root_squash fsid=0
hostA,/other/path,hostname1.my.domain,rw sync no_root_squash fsid=0
hostA,/other/path,hostname2.my.domain,rw sync no_root_squash fsid=0
Above "hostA" is the localhost hostname where the script is ran.

What I'm doing with Perl:
Code:
...
open(DAT,"/etc/exports") or die "Can't open: $!\n";
@info = <DAT>;
close(DAT);
foreach $key (@info){
...
I thought it may be simpler with awk, but I usually only do one-liners. Is there a way to get my desired output using awk?

Thanks.
 
Hi

Code:
awk '/^[^ ]/{p=$1;$1=""}{sub(/^ */,"");sub(/\(/,",");gsub(/,/," ");sub(/\).*/,"");print ENVIRON["HOSTNAME"]","p","$0}' /etc/exports
Tested with [tt]gawk[/tt] and [tt]mawk[/tt]. Not sure if the [tt]ENVIRON[/tt] array is available in all [tt]awk[/tt] implementations. If you have problem with it, tell us.


Feherke.
 
Hi

blarneyme said:
That's shorter than my 38 lines of Perl. :)
[tt][blue][small][ignore][off-topic][/ignore][/small][/blue][/tt]
But longer than my Perl code...
Code:
perl -nae 'if(/^\S/){$p=$F[0];s/^\S+\s+//};s/^ *//;s/\(/,/;s/,/ /g;s/\).*//;print"$ENV{HOSTNAME},$p,$_"' 
/etc/exports
Note that the above is an approximate rewrite of the AWK code. But the Perl code can be reduced even more :
Code:
perl -pae '$p=$F[0]if/^\S/;s/^\S+\s+//;s/^ *//;s/\(/,/;s/,/ /g;s/\).*//;$_="$ENV{HOSTNAME},$p,$_"' /etc/exports
[tt][blue][small][ignore][/off-topic][/ignore][/small][/blue][/tt]

Feherke.
 
I need to read much more on regular expressions!

Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top