Numbering by field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numbering by field
# 1  
Old 11-17-2014
Numbering by field

I'm not really sure how to explain this but I will try. In the attached file if $4=$4 and $5="-" then the last record is 1 and the one above that is 2, etc...

However, $4=$4 and $5="-" then the first record is 1 and the one below that is 2, etc...

Code:
"-" example:

chr10 90694830 90695123 ACTA2 - 10
chr10 90697817 90697999 ACTA2 - 9
chr10 90699263 90699455 ACTA2 - 8
chr10 90700985 90701147 ACTA2 - 7
chr10 90701541 90701626 ACTA2 - 6
chr10 90703553 90703664 ACTA2 - 5
chr10 90707014 90707143 ACTA2 - 4
chr10 90708558 90708710 ACTA2 - 3
chr10 90712487 90712580 ACTA2 - 2
chr10 90750695 90751147 ACTA2 - 1

Code:
 "+" example
chr11 119076985 119077322 CBL + 1
chr11 119103157 119103405 CBL + 2
chr11 119142444 119142591 CBL + 3
chr11 119144577 119144734 CBL + 4
chr11 119145541 119145663 CBL + 5
chr11 119146706 119146844 CBL + 6
chr11 119148466 119148554 CBL + 7
chr11 119148875 119149007 CBL + 8
chr11 119149219 119149423 CBL + 9
chr11 119155678 119155810 CBL + 10
chr11 119155898 119156276 CBL + 11
chr11 119158561 119158656 CBL + 12
chr11 119167627 119167744 CBL + 13
chr11 119168093 119168191 CBL + 14
chr11 119169067 119169250 CBL + 15
chr11 119170204 119178859 CBL + 16

Thank you Smilie.
# 2  
Old 11-17-2014
You said
Code:
However, $4=$4 and $5="-"

But, did you mean
Code:
However, $4=$4 and $5="+"

# 3  
Old 11-17-2014
Yes, you are correct. Thank you very much Smilie and I apologize for any confusion.
# 4  
Old 11-17-2014
Code:
awk 'BEGIN { prev="";cur="";cursign="";prevsign="";}
{
if($5!="")
cursign=$5;
cur=$4;
#print "prev =" prev " cur= " cur " cursign= " cursign " prevsign=" prevsign

if($5=="-")
        {
        if(cur!=prev)
                {
		if(prevsign=="+" || prevsign=="")
			{
			i=1;	
			a[i]=$0
			}
		else
			{		
			k=i;
			for(j=1;j<=k;j++)
				{
				print a[j] " " i
				i=i-1;
				}
	                i=1;
			a[i]=$0
                	}
		}
        else
                {
		i=i+1
		a[i]=$0
                }
		prev=cur;
        }
if($5=="+")
        {
        if(cur!=prev)
                {
		if(prevsign=="-" || prevsign=="")
	  		{
			k=i;
			for(j=1;j<=k;j++)
				{
				print a[j] " " i
				i=i-1;
				}
			i=1;
                	print $0 " " i
			}	
		else
			{
                	i=1;
                	print $0 " " i
                	}
		}
        else
                {
                i=i+1
                print $0 " " i
                }
prev=cur;
	}
prevsign=$5;
}
END { 
if(cursign=="-")
	{
			k=i;
			for(j=1;j<=k;j++)
				{
				print a[j] " " i
				i=i-1;
				}
	}
}
' input.txt

# 5  
Old 11-18-2014
I seem to be getting the below error with the code. Thank you Smilie.

Code:
awk -f exon.awk
awk: exon.awk:2: awk 'BEGIN { prev="";cur="";cursign="";prevsign="";}
awk: exon.awk:2: ^ invalid char ''' in expression
awk: exon.awk:2: awk 'BEGIN { prev="";cur="";cursign="";prevsign="";}
awk: exon.awk:2: ^ syntax error

exon.awk
Code:
 #!/bin/awk -f
awk 'BEGIN { prev="";cur="";cursign="";prevsign="";}
{
if($5!="")
cursign=$5;
cur=$4;
#print "prev =" prev " cur= " cur " cursign= " cursign " prevsign=" prevsign
if($5=="-")
        {
        if(cur!=prev)
                {
  if(prevsign=="+" || prevsign=="")
   {
   i=1; 
   a[i]=$0
   }
  else
   {  
   k=i;
   for(j=1;j<=k;j++)
    {
    print a[j] " " i
    i=i-1;
    }
                 i=1;
   a[i]=$0
                 }
  }
        else
                {
  i=i+1
  a[i]=$0
                }
  prev=cur;
        }
if($5=="+")
        {
        if(cur!=prev)
                {
  if(prevsign=="-" || prevsign=="")
     {
   k=i;
   for(j=1;j<=k;j++)
    {
    print a[j] " " i
    i=i-1;
    }
   i=1;
                 print $0 " " i
   } 
  else
   {
                 i=1;
                 print $0 " " i
                 }
  }
        else
                {
                i=i+1
                print $0 " " i
                }
prev=cur;
 }
prevsign=$5;
}
END { 
if(cursign=="-")
 {
   k=i;
   for(j=1;j<=k;j++)
    {
    print a[j] " " i
    i=i-1;
    }
 }
}
' output.txt > exon_number.txt


Last edited by Don Cragun; 11-18-2014 at 03:17 PM.. Reason: Get rid of extraneous SIZE tags.
# 6  
Old 11-18-2014
It has to do with the way you are executing the awk. if you run the awk on command line it is working fine. I am not expert either on how to run it by placing the code in a file. May be some experts here can suggest on how to run it !
# 7  
Old 11-18-2014
Yes your code runs fine ysrv1..
Some users read the man pages before trying anything, some dont and invent impossible syntax...

To get your code to run as a file for awk meaning awk will be using awk -f awkprgfile...

your code copied in awkprgfile should have first line modified and last line removed or commmented out, giving first line and last line:
Code:
BEGIN { prev="";cur="";cursign="";prevsign="";}
.
.
#  ' input.txt

and so to run:
Code:
 awk -f awkprgfile  input.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing numbering from last character

Hi, I have a file test.txt, below the contents: SCDE1 SF9 STR1D2 SREDF21 FRED STER2R4 I want to remove only the last character if it is the number, so the output should as below: SCDE SF STR1D SREDF2 FRED STER2R (4 Replies)
Discussion started by: khchong
4 Replies

2. Shell Programming and Scripting

Incremental numbering?

Would it be possible for a script to duplicate a file and incrementally number it? File in: XXX_007_0580_xxxx_v0016.aep File out: XXX_007_0580_xxxx_v0017.aep If someone knows of a way I'd love to see it. Thanks! (7 Replies)
Discussion started by: scribling
7 Replies

3. Shell Programming and Scripting

help with numbering a file

Hi, All I need to do is number a file. The file looks like this > JJJJJJJJJJJJJJJJJJJJJ > JKJKJKKKKKKJJJ > MMMMYKKKJKKK what I want to do is number it so that theres a numerical value beside the >. >1 JJJJJJJJJJJJJJJJJJJJJ >2 JKJKJKKKKKKJJJ (2 Replies)
Discussion started by: kylle345
2 Replies

4. UNIX for Dummies Questions & Answers

Numbering the rows

If I a list of components, is there anyway to number (like automatically have: 1,2,3,...) the rows of my data? (1 Reply)
Discussion started by: cosmologist
1 Replies

5. Shell Programming and Scripting

Numbering duplicates

Hi, I have this large file and sometimes there are duplicates and I want to basically find them and figure how many there are. So I have a file with multiple columns and the last column (9) has the duplicates. eg. yan tar tar man ban tan tub tub tub Basically what I want to... (6 Replies)
Discussion started by: kylle345
6 Replies

6. Shell Programming and Scripting

Numbering Lines

Hello everyone, I want get numbered lines from a file. and i can do it with: sed = file.txt | sed "/./N; s/\n/ /" | sed -n "5,7p" but the output that i get is something similar to: 5 line5 6 line6 7 line7 and i want something like this (with 2points after the number): 5:... (6 Replies)
Discussion started by: vibra
6 Replies

7. UNIX for Advanced & Expert Users

numbering blanks

hello i'm trying to figure out how to number a blank line. For instance this : sed '/./=' file | sed '/./N; s/\n/ /' gives me 1 aaaa 2 bbbbbb 4 cccccc 5 ffkkkfff 6 ffsdfdfs I would like something like this: 1 aaaaa 2 3 bbbbbb 4 5 cccccc And so... (6 Replies)
Discussion started by: wisher115
6 Replies

8. Shell Programming and Scripting

Numbering

I'm trying to do a script that will look for a log file if it is already there change the name to another name. I.E if log.0 is there rename to log.1 rename log.1 to log.2 rename log.2 to log.3 and so on. Only thing is I got no idea where or what is the best command to use for this? ... (3 Replies)
Discussion started by: merlin
3 Replies

9. UNIX for Dummies Questions & Answers

Numbering!

Just a shot question... how to make 1,2,3,...999 into the form of 001,002,003....999 (3 digits) Thanks.... (9 Replies)
Discussion started by: biglemon
9 Replies

10. UNIX for Dummies Questions & Answers

numbering of process

:confused: How does UNIX handle the numbering of processes? (2 Replies)
Discussion started by: tweety111
2 Replies
Login or Register to Ask a Question

Featured Tech Videos