Selective grouping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selective grouping
# 1  
Old 06-07-2012
Selective grouping

I have a text file in this format.
Code:
[server1.domain.com]
Group: AAA
Notes: IP : 11.11.11.11

#User xxxxxxxxx

#Password aaaaaaaaaaaaaaaa


[server2.domain.com]
Group: AAA
Notes: IP : 11.11.11.22

#User yyyyyyyyyyyyy

#Password bbbbbbbbbbbbb



[server3.domain.com]
Group: AAA
Notes: IP : 11.11.11.33

#User zzzzzzzzzzz

#Password cccccccccccccccc

I need a script/sed/awk that when applied on the above test file, it will print as follows.

Code:
xxxxxxxxx@11.11.11.11
yyyyyyyyyyyyy@11.11.11.22
zzzzzzzzzzz@11.11.11.33

# 2  
Old 06-07-2012
Try this:

Code:
awk '
    /Notes:/ { ip = $NF; next; }
    /User/  { printf( "%s@%s\n", $NF, ip ); next; }
' input-file

This User Gave Thanks to agama For This Post:
# 3  
Old 06-08-2012
Code:
# awk '{print $9"@"$7}' RS="\n\n\n" text
xxxxxxxxx@11.11.11.11
yyyyyyyyyyyyy@11.11.11.22
zzzzzzzzzzz@11.11.11.33

This User Gave Thanks to ygemici For This Post:
# 4  
Old 06-08-2012
Note: In standard awk RS can only be single character. Only in gawk and mawk can RS also be a regex (so it can also be multiple characters).

Last edited by Scrutinizer; 06-08-2012 at 05:15 AM..
# 5  
Old 06-08-2012
and the sed
Code:
# sed -n '/Notes\|User/{N;s/\n//;s/.* //;N;s/\(.*\)\n.* \(.*\)/\2@\1/gp}' text
xxxxxxxxx@11.11.11.11
yyyyyyyyyyyyy@11.11.11.22
zzzzzzzzzzz@11.11.11.33

This User Gave Thanks to ygemici For This Post:
# 6  
Old 06-08-2012
Thanks, its working.

A bit modification needed. The output should be formatted as
Code:
alias server1.domain.com='ssh xxxxxxxxx@11.11.11.11' 
alias server2.domain.com='ssh yyyyyyyyyyyyy@11.11.11.22' 
alias server3.domain.com='ssh zzzzzzzzzzz@11.11.11.33'

# 7  
Old 06-09-2012
Code:
awk '/IP/{P=$NF;i=1};i&&/#User/{print $2"@"P;i=0}' filename
xxxxxxxxx@11.11.11.11
yyyyyyyyyyyyy@11.11.11.22

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with grouping and zipping

Hi can you please help with the below ? source file: Column1,Column2,Column3,Column4 abc,123,dir1/FXX/F19,1 abc,123,dir1/FXX/F20,1 abc,123,dir1/FXX/F23,2 abc,123,dir1/FXX/C25,2 abc,123,dir1/FXX/X25,2 abc,123,dir1/FXX/A23,3 abc,123,dir1/FXX/Z25,3 abc,123,dir1/FXX/Y25,4 I want to... (3 Replies)
Discussion started by: paul1234
3 Replies

2. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

UNIX grouping

Hi guys, I am a complete newbie to unix and have been tasked with creating a script to group the following data (file) by hourly slots so that I can count the transactions completed within the peak hour. I am not sure how to group data like this in unix. Can anyone please help? Here is an... (1 Reply)
Discussion started by: MrMidas
1 Replies

4. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

5. UNIX for Dummies Questions & Answers

Grouping in grep

How do you do grouping in grep? Here's how I tried it at first: egrep 'qualit(y|ies)' /usr/share/dict/words -bash: syntax error near unexpected token `(' I'm using GNUgrep, and I found this on their site. grep regular expression syntax So I tried this: egrep 'qualit\(y\|ies\)'... (2 Replies)
Discussion started by: sudon't
2 Replies

6. UNIX for Dummies Questions & Answers

Help with selective ls

Hi all :wall: Can anyone advise how do I use ls to do a selective amd sorted listing of file that I want to have as below? Am looking for files that are named as log_<nnnn>.txt, where <nnnn> are numeric, i.e. I want to have a listing sorted from the newest to the oldest of files that... (7 Replies)
Discussion started by: newbie_01
7 Replies

7. UNIX for Dummies Questions & Answers

Help with data grouping

Hi all, I have a set data as shown below, and i would like to eliminate the name that no children - boy and girl. What is the appropriate command can i use(other than grep)? Please assist... My input: name sex marital status children - boy children - girl ... (3 Replies)
Discussion started by: 793589
3 Replies

8. UNIX for Dummies Questions & Answers

grouping and sorting??

how would I write a command line that creates a new file named stuff.txt in the current working directory which contains the number of directories in the current working directory, followed by the number of empty files in the current working directory, followed by the name of the working directory? (3 Replies)
Discussion started by: jorogon0099
3 Replies

9. UNIX for Advanced & Expert Users

Regarding the user grouping

I am new to the unix server. My question is: i have 10 users a b c d e f g h i j Among the 10 5 users are (normal) user and remainging 5 users are (qad) users How can i sepearte this one? How can i give the dlc access rights? Please provide the clear cut idea. (0 Replies)
Discussion started by: kingganesh04
0 Replies

10. Shell Programming and Scripting

egrep and grouping

i am using the c shell on solaris. directories i'm working with: ls -1d DIV* DIV_dental/ DIV_ibc/ DIV_ifc/ DIV_index/ DIV_pharm/ DIV_sectionI/ DIV_sectionI-title/ DIV_sectionI-toc/ DIV_sectionII-title/ DIV_sectionII-toc/ DIV_standing/ DIV_standing-toc/ DIV_title/ DIV_vision/ (1 Reply)
Discussion started by: effigy
1 Replies
Login or Register to Ask a Question