The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert a pipe delimited file to a':" delimited file priyanka3006 Shell Programming and Scripting 6 05-26-2009 10:53 AM
Converting Space delimited file to Tab delimited file jeevs81 UNIX for Dummies Questions & Answers 16 02-26-2009 02:49 AM
Determining type of file JWilliams UNIX for Dummies Questions & Answers 2 04-20-2007 03:14 AM
Determining file length jbrubaker UNIX for Dummies Questions & Answers 1 07-07-2006 04:06 PM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 09:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
phil_heath phil_heath is offline
Registered User
  
 

Join Date: May 2009
Posts: 54
Determining position in a tab delimited file

hi, I want to determine the position of specific values over a cutoff.

So I have a string of values that are mainly negative in number and I want to print the rare few that are positive. Specifically I want to know the position of the value along the string. The position is based from right to left (the right most value is 1 etc..). At the same time it should ignore the UI's at the end.

e.g. (note the file is tab delimited)
Code:
name  x  -0.002   -92  -45  78.33  UI  UI  UI
name  y  -9.55    -83.21  45  -9.34  UI  UI  UI
I want the output to look like this

Code:
name  x  1
name  y  2
kinda tricky
  #2 (permalink)  
Old 2 Weeks Ago
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,516
where's your code? you know about awk or the shell already from previous posts. So try to do it yourself first. Read the docs, always!!
  #3 (permalink)  
Old 2 Weeks Ago
pravin27 pravin27 is offline
Registered User
  
 

Join Date: Sep 2009
Location: UAE
Posts: 38
Hi,
Try this,

Code:
#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t",$_,"\n";
        }
        }
        }
}
perl test27.pl sample.txt

Output
Code:
name    x       1       78.33
name    y       2       45
  #4 (permalink)  
Old 2 Weeks Ago
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Is this homework or are you working on the same project than this poster?
parsing rows
  #5 (permalink)  
Old 2 Weeks Ago
phil_heath phil_heath is offline
Registered User
  
 

Join Date: May 2009
Posts: 54
hey this code works great but whenever it runs into a line with multiple positives, it gives an output that looks like this..

Code:
name    x       1       78.33
1     56
name    y       2       45
how would I modify the below code so I get the name and x representing the outputs

Code:
name    x       1       78.33
name    x       1       56
Code:
#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t","$_","\n";
        }
        }
        }
}
I tried modifying it but no success.
  #6 (permalink)  
Old 2 Weeks Ago
pravin27 pravin27 is offline
Registered User
  
 

Join Date: Sep 2009
Location: UAE
Posts: 38
Modiefied, try this ..

Code:
#!/usr/bin/perl
while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t","$_","\n";
        last;
        }
        }
        }
}
input file
Code:
name  x  -0.002   -92  -45  78.33  UI  UI  UI
name  y  -9.55    -83.21  33    45  -9.34  UI  UI  UI
output
Code:
name    x       1       78.33
name    y       2       45
  #7 (permalink)  
Old 2 Weeks Ago
phil_heath phil_heath is offline
Registered User
  
 

Join Date: May 2009
Posts: 54
Quote:
Originally Posted by pravin27 View Post
Hi,
Try this,

Code:
#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t",$_,"\n";
        }
        }
        }
}
perl test27.pl sample.txt

Output
Code:
name    x       1       78.33
name    y       2       45

Hey actually the original script worked better. So the output using the original script would give.

Code:
name    x    1    78.33
name    y    2    45
3    33
That is perfectly correct but I want it to have

name x 1 78.33
name y 2 45
name y 3 33

i have no clue how to add the name and y (or x) for the other values.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0