Complex positioning

I got these entries in a file

alias server.domain.com='ssh 192.168.1.1@user1 '
alias server1.domain.com='ssh user2 @192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'

In the above lines, last line is the correct format. The first is IP@username, has got a space after the user1. In the second entry, there is space after user2. If its IP@user, it should be user@IP after the sed/awk operation.

Is there a question here?

1 Like

Hello Anil510,

Following may help you in same.

awk -vs1="'" -F"ssh " '{sub(/[[:space:]]/,X,$2);if($2 ~ /^[[:digit:]]/){W=V=$2;gsub(/.*@/,X,W);gsub(s1,Z,W);gsub(/@.*/,Y,V);$2=W"@"V s1};print}' OFS="ssh " Input_file

Output will be as follows.

alias server.domain.com='ssh user1@192.168.1.1'
alias server1.domain.com='ssh user2@192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'
 

EDIT: Adding a non one liner form for solution on same.

 awk -vs1="'" -F"ssh " '{
                                       sub(/[[:space:]]/,X,$2);
                                                                            if($2 ~ /^[[:digit:]]/){
                                                                            W=V=$2;
                                                                            gsub(/.*@/,X,W);
                                                                            gsub(s1,Z,W);
                                                                            gsub(/@.*/,Y,V);
                                                                            $2=W"@"V s1
                                                                                                    };
                                       print
                       }' OFS="ssh " Input_file

NOTE: Following will work if the data is in same format as you have shown to us.

Thanks,
R. Singh

$ sed "s/ *@/@/;s/\(\([0-9]\{1,\}.\{0,1\}\)\{4\}\)@\([^ ']*\)/\3@\1/" file
alias server.domain.com='ssh user1@192.168.1.1 '
alias server1.domain.com='ssh user2@192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'

Hi,

Try this:

cat one
alias server.domain.com='ssh 192.168.1.1@user1 '
alias server1.domain.com='ssh user2 @192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'

awk -F'=' '{gsub(/ /,X,$2);gsub("ssh","& ",$2);print $0}' one
alias server.domain.com 'ssh 192.168.1.1@user1'
alias server1.domain.com 'ssh user2@192.168.1.1'
alias server1.domain.com 'ssh user3@192.168.1.1'

Assuming that these are to turn into ssh commands, we need to reverse the first line to be this:-

alias server.domain.com 'ssh user1@192.168.1.1'

It would be better to know the actual requirement first before we leap into this though.

Robin

Hi Robin,

Thank you for pointing out. Missed that.:frowning:

awk -F'=' '{q="\047";gsub(/[\047 ]|ssh/,X,$2);split($2,S,"@");if (S[2] !~ /^[0-9]/){t=S[2];S[2]=S[1];S[1]=t};print $1,FS,q,"ssh ",S[1],"@",S[2],q}' OFS="" one
alias server.domain.com='ssh user1@192.168.1.1'
alias server1.domain.com='ssh user2@192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'

Hope this helps

We can leave the space after the IP address. That will not matter for the ssh command.

sed "s/ \([0-9.]\{1,\}\)@\([^ ']*\)/ \2@\1/ ; s/ @/@/"  file

GNU sed -r; BSD sed -E:

sed -E "s/ ([0-9.]+)@([^ ']*)/ \2@\1/ ; s/ @/@/" file
alias server.domain.com='ssh user1@192.168.1.1 '
alias server1.domain.com='ssh user2@192.168.1.1'
alias server1.domain.com='ssh user3@192.168.1.1'

It won't take core of switching the order on the first line, but the extra space problem can be resolved simply with:

awk '{print $1,$2,$3 $4}' file
1 Like

Binary1 its working fine. Thanks

1 Like