[SOLVED] Sorting file and get everything on same line on condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Sorting file and get everything on same line on condition
# 1  
Old 06-05-2012
[SOLVED] Sorting file and get everything on same line on condition

Good afternoon!

I am a perl newbie. I hope you will be patient with me.

I have a script that needs to be written in perl. I can't do it in awk or shell scripting.

Here is the script:

Code:
#!/usr/bin/perl 
use POSIX qw(strftime); 
use FileHandle; 
use Getopt::Long; 
use IO::Handle; $MYSQL='/pathtomysql/mysql';  

my ($dbh,$CONTAINER,$DEBUG,$DOMAIN,$DOMTYPE,$USER,$PASSWORD,$USAGE);  
$Getopt::Long::autoabbrev=1; &GetOptions('-zdebug'=>\$DEBUG,'container:s'=>\$CONTAINER,'-user:s'=>\$USER,'-password:s'=>\$PASSWORD,'-x'=>\$USAGE,'v'=>\$VERSION);

if ($VERSION) {    
   print "Version 1.0 Build date: 23 May 2012 \n";    
   exit; 
}  

if (! $CONTAINER) {    
   print "missing container: $CONTAINER \n";    
   &usage; 
}  

if (! $USER) {    
   print "missing user: $USER\n";    
   &usage; 
}  

if (! $PASSWORD) {    
   print "missing password: $PASSWORD\n";    
   &usage; 
}  

if ($USAGE) {    
   &usage; 
} 
print "container: $CONTAINER \n" if $DEBUG;  

# This is converting it to all uppercase 
$CONTAINER =~ tr/a-z/A-Z/;  

my $TMPSQLCONT="/tmp/sql"; 
my $TMPCONTLIST="/tmp/sql.contlist";  

# Getting the container ID's for the subnets that we want to get data on   
$SQLCONT="select user_defined_fld_value.objectid, inet_ntoa(block.STARTADDR),  
user_defined_fld.tag, user_defined_fld_value.VALUE  from user_defined_fld_value  
left join user_defined_fld  on user_defined_fld_value.USER_DEFINED_FLD_ID=user_defined_fld.id  
left join container_block  on container_block.blockid=user_defined_fld_value.objectid  
left join block  on block.id=container_block.blockid  
left join container  on container.id=container_block.containerid  
where container.notes like '%$CONTAINER%'";      

my $tmpcont=FileHandle->new;     
$tmpcont->open(">$TMPSQLCONT");     
print $tmpcont "$SQLCONT;\n";     
$tmpcont->close;     
system("$MYSQL -u $USER -p$PASSWORD instancename <$TMPSQLCONT > $TMPCONTLIST"); 

# Deleting file afterwards
unlink($TMPSQLCONT);

# Informing where the final file is located

print "Your file is located at $TMPCONTLIST\n"; 

######################################################################### 
sub usage {    
    print "Usage: \n";    
    print "      -c:  Container \n";    
    print "      -u:  login \n";    
    print "      -p:  password\n";    
    print "      -z:  debug \n";    
    print "      -x:  This message \n";    exit 0; 
} 
#########################################################################

This is a sample of the $TMPSQLCONT (it is tab delimmited):

Code:
9885    10.10.9.48    Room    1105A 
9885    10.10.9.48    Jack    1105A--05D 
9885    10.10.9.48    org_code        B703 
9885    10.10.9.48    Building        1268A 
114948  10.10.184.0    google_nets       off 
114948  10.10.184.0    blockSecName    test name 
114948  10.10.184.0    blockTechName   brian test 
114948  10.10.184.0    blockAdminName  test admin 
114949  10.10.184.0    blockSecName    John G. Smooth 
114949  10.10.184.0    blockTechPhone  222-555-1212 
114949  10.10.184.0    blockAdminName  Lucy P. Wallice 
114949  10.10.184.0    blockAdminId    8878787 
114949  10.10.184.0    block_name      unknown 
114949  10.10.184.0    blockSecId      787878 
114949  10.10.184.0    blockAdminEmail lucy.p.wallice@google.com 
114949  10.10.184.0    blockTechName   TEST LAN 
114949  10.10.184.0    blockSecPhone   222-555-3232 
114949  10.10.184.0    blockTechEmail  terCInternal@google.com 
114949  10.10.184.0    blockSecEmail   John.goody@google.com 
114949  10.10.184.0    google_nets       off

How can I go through this and split based on tabs, then join it based on the second field? Like this:

Code:
10.10.9.48|Room=1105A|Jack=1105A--05D|org_code=B703|Building=1268A

10.10.184.0|google_nets=off|blockSecName=test name|blockTechName=brian test|blockAdminName=test admin|blockSecName=John G. Smooth|blockTechPhone=222-555-1212|blockAdminName=Lucy P. Wallice|blockAdminId=8878787|block_name=unknown|blockSecId=787878|blockAdminEmail=lucy.p.wallice@google.com|blockTechName=TEST LAN|blockSecPhone=222-555-3232|blockTechEmail=terCInternal@google.com|blockSecEmail=John.goody@google.com|google_nets=Sof

---------- Post updated at 07:08 PM ---------- Previous update was at 04:04 PM ----------

Got it to work. Added this to the end:

Code:
my $TMPCONTLISTFINAL="/tmp/sql.contlist.final";

 open (FILE2, ">$TMPCONTLISTFINAL") || die "problem opening $TMPCONTLISTFINAL\n"
;
 open (FILE, $TMPCONTLIST);
my %output;
    while (<FILE>) {
    chomp( my ( $ip, $fld2, $fld3 ) = ( split /\t/ )[ 1 .. 3 ] );
    push @{ $output{$ip} }, "$fld2=$fld3";  }

print FILE2 "$_|" . join( '|', @{ $output{$_} } ) . "\n\n" for sort keys %output
;
     close(FILE);
     close(FILE2);

# Deleting file afterwards
unlink($TMPSQLCONT);
unlink($TMPCONTLIST);

Moderator's Comments:
Mod Comment edit by bakunin: changed thread title accordingly.

Last edited by bakunin; 06-05-2012 at 09:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Help with condition

I have below code, I am checking on one server where ppp.sh process not running it is perfectly fine, when i am checking the same script on another server where ppp.sh script is running, it is throwing error "too many arguments", how to avoid this. If i will check on server where process running it... (5 Replies)
Discussion started by: learnbash
5 Replies

2. UNIX for Dummies Questions & Answers

[Solved] New Line in file

Hi, Though I was successful in following query, I like to know the other ways of doing it. I have a file that is sent as an attachment via mail. However, while opening it, notepad does not recognize new line character whereas other editors like text pad recognizes new line character of unix.... (2 Replies)
Discussion started by: bobbygsk
2 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. Shell Programming and Scripting

ip 2 country sorting[solved]

Dear folks. I have list of ip address from different country, i would like check from script this ip address belong to which country, please suggest any command, i have more than 200 ip address so i need to do it via script. Script will show me country code and its City if possible? ... (0 Replies)
Discussion started by: learnbash
0 Replies

5. Shell Programming and Scripting

While loop is not reading next line in the file when IF condition is used.

Hi Guys I am new to scripting.Please forgive for asking basic questions. I want to write a script to check whether the logs are getting updated in last 15 mins. cat server 192.168.1.6 192.168.1.7 cat list 192.168.1.7 /logs/logpath1 192.168.1.7 /logs/logpath2 192.168.1.6... (4 Replies)
Discussion started by: vdurai
4 Replies

6. Shell Programming and Scripting

[Solved] Sorting by several fields

Hello, I have a file with information separated by ";" like this: ABC;20110126000008;00-10-95-29-17-C6;2;37190292 ABC;20110126000008;00-10-95-29-17-C6;1;53140866 ABC;20110126000008;00-10-05-01-11-38;2;11182251 ABC;20110126000008;00-10-05-01-11-38;1;25952816... (3 Replies)
Discussion started by: rubber08
3 Replies

7. Shell Programming and Scripting

[Solved] Read a .gz file line by line without using gzcat

Hi all Is there a way to read and process a gzip file line by line similar to a text file without using gzcat.. while processing a text file we will usually use the logic exec<sample.txt while read line do echo $line done Is there a similar way to process the gz file in the same... (4 Replies)
Discussion started by: aikhaman
4 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Cutting specific line of a file by checking condition

testfile.csv 0","1125209",,"689202CBx18888",,"49",,,"NONMC",,,,,"01112010",,,,,,,"MTM- "1","",,"689202ABx19005",,"49",,,"NONMC",,,,,"01072010",,,,,,,"MTM- testfile.csv looks like above format if the second column is null then get 23rd column and store in a different varible .. add all the... (1 Reply)
Discussion started by: mgant
1 Replies

10. UNIX for Dummies Questions & Answers

Help: Sorting and extracting a line from a file

I was proposed with the following problem: The file 'numbers' contains a list of numbers. Write a command to place the largest one of those numbers in the file 'largest' (there should be nothing else in that file). Do not use the 'head' command in your answer. I think if i used the sort... (1 Reply)
Discussion started by: stava
1 Replies
Login or Register to Ask a Question