Complex positioning


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex positioning
# 1  
Old 12-08-2014
Complex positioning

I got these entries in a file

Code:
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.
# 2  
Old 12-08-2014
Is there a question here?
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 12-08-2014
Hello Anil510,

Following may help you in same.

Code:
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.
Code:
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.
Code:
 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

Last edited by RavinderSingh13; 12-08-2014 at 10:32 AM.. Reason: Added user@ip logic for first column now
# 4  
Old 12-08-2014
Code:
$ 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'

# 5  
Old 12-08-2014
Hi,

Try this:

Code:
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'

# 6  
Old 12-08-2014
Assuming that these are to turn into ssh commands, we need to reverse the first line to be this:-
Code:
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
# 7  
Old 12-08-2014
Hi Robin,

Thank you for pointing out. Missed that.Smilie

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

positioning words in a line

I have problem with this for last few days and I would really be grateful if you can help me :wall::wall::wall::wall::wall: I have sets of files in one directory, and each of them have few lines each one of these lines has 1 or more occurrence of words Y = I need to find the position of the... (10 Replies)
Discussion started by: A-V
10 Replies

2. Shell Programming and Scripting

Tab positioning

xx00102 1(SG): xx00102 3(SG): xx00115 lan900: xx00115 lan901: xx00116 1(SG): xx00116 3(SG): the boxes are echoed on the same line with one \t. Is there anyway to get all the boxes to line up? Or possibly is there a way to fix the position of something... (3 Replies)
Discussion started by: trimike
3 Replies

3. Shell Programming and Scripting

cursor positioning

Hi All, please help me to know how to move the cursor to the desired position? For example, in a shell script, I am displaying echo "\t Enter your Name:" please help me how to move cursor near the first word. for example, if the output is as below ... (3 Replies)
Discussion started by: little_wonder
3 Replies

4. UNIX for Dummies Questions & Answers

Mouse clicking/positioning in Terminal

Is is possible to use the mouse in Terminal on OS X Leopard? I thought I read somewhere in the book that I have that it is, but I can't find it. I'd like to use it to click in one of the editors if I have to. (0 Replies)
Discussion started by: Straitsfan
0 Replies

5. Programming

positioning cursor

I am using curses.h and signals.h to control output to screen. My code displays an unchanging prompt that waits for user input. Meanwhile alarm signals are being generated that cause other ancillary messages to appear at other locations on the screen at various times. The problem I have is with... (2 Replies)
Discussion started by: enuenu
2 Replies

6. UNIX for Dummies Questions & Answers

Cursor Positioning

Can anyone tell me how to ouput the current cursor coordinate? I have tried using tput sc and tput rc. However I want to know what the coordinate is. Thanks. (1 Reply)
Discussion started by: bestbuyernc
1 Replies

7. UNIX for Dummies Questions & Answers

Positioning curser at EOF in vi

Hi there, Is there a way to position the cursor at EOF in vi, I know from the man pages how to position the cursor at the end of a paragraph, but what about the EOF (End Of File). Regards (6 Replies)
Discussion started by: JimJim
6 Replies

8. UNIX for Dummies Questions & Answers

Cursor positioning thru VI Editor

Is there a way that I can position cursor at say line 23, column 2? Thank you in advance. (8 Replies)
Discussion started by: Latha Nair
8 Replies
Login or Register to Ask a Question