I have this in a file
[server.domain.com]
11.22.33.44
yyyyyyuser
With awk/sed, I need this to be output as follows
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
I have this in a file
[server.domain.com]
11.22.33.44
yyyyyyuser
With awk/sed, I need this to be output as follows
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
anil510,
Check this out:
$ cat file |sed 's/\[//g;s/\]//g'|xargs|while read a b c;do printf "alias $a=\'ssh $c@$b\'\n";done
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
Enjoy!,
Try also
awk '{gsub (/[][]/,""); print "alias "$1"=\047ssh "$3"@"$2"\047"}' RS="" file
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
Rudic, it works.
rveri, Your solution works if there only one server.
Using Perl, file022 :
[server.domain.com]
11.22.33.44
yyyyyyuser
[server2.domain.com]
55.66.77.88
zzzzzzuser
program file022.pl :
#!/usr/bin/perl -w
use strict;
my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";
my ($record,$srv,$ip);
my $cpt=1;
open(FILEIN,"<$filename") or die"open: $!";
while( defined( $record = <FILEIN> ) ) {
chomp $record;
if($cpt == 1) {
($srv) = $record =~ m/(\w+\.\w+\.\w+)/ ;
}
elsif($cpt == 2) {
$ip=$record;
}
else {
print "alias $srv='ssh $record\@$ip'\n";
}
$cpt++;
$cpt=1 if($cpt == 4);
}
close(FILEIN);
execution :
%./file022.pl file022
alias server.domain.com='ssh yyyyyyuser@11.22.33.44'
alias server2.domain.com='ssh zzzzzzuser@55.66.77.88'