Read 4th column and print those many rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read 4th column and print those many rows
# 1  
Old 08-18-2014
Read 4th column and print those many rows

Hi,

My input file

Code:
chr1	3217769	3217789	2952725-5	255	+
chr1	3260455	3260475	2434087-6	255	-

My desired output

Code:
chr1	3217769	3217789	2952725-1	255	+
chr1	3217769	3217789	2952725-2	255	+
chr1	3217769	3217789	2952725-3	255	+
chr1	3217769	3217789	2952725-4	255	+
chr1	3217769	3217789	2952725-5	255	+
chr1	3260455	3260475	2434087-1	255	-
chr1	3260455	3260475	2434087-2	255	-
chr1	3260455	3260475	2434087-3	255	-
chr1	3260455	3260475	2434087-4	255	-
chr1	3260455	3260475	2434087-5	255	-
chr1	3260455	3260475	2434087-6	255	-

So basically I want to read the number after the dash in 4th column.

Then print that entire row starting at 1 until the number that is after the dash.

Thanks in advance.
# 2  
Old 08-18-2014
Try this:

Code:
awk '{v=substr($0,index($0,"-")+1,1); for (x=1; x<=v+0; x++)  {sub("-"v,"-"x);sub("-"x-1,"-"x);print $0}}' inputfile

This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 08-18-2014
Quote:
Originally Posted by pilnet101
Try this:

Code:
awk '{v=substr($0,index($0,"-")+1,1); for (x=1; x<=v+0; x++)  {sub("-"v,"-"x);sub("-"x-1,"-"x);print $0}}' inputfile

Thank you. Worked perfectly.

I just saw it that if the digits are more than 1 after the dash, the command is not considering it.

For ex:

The following examples are not being considered

Code:
chr1	3217769	3217789	2952725-15 255	+
chr1	3217769	3217789	2952725-555	255	+
chr1	3217769	3217789	2952725-05	255	+

Thanks again.

Last edited by jacobs.smith; 08-18-2014 at 12:48 PM..
# 4  
Old 08-18-2014
Try this one now:

Code:
awk 'match($0,/-[0-9]+ */){v=substr($0,RSTART+1,RLENGTH-1)}{for (x=1; x<=(v+0); x++) {sub("-"v,"-"x);sub("-"x-1,"-"x);print}}'

This User Gave Thanks to pilnet101 For This Post:
# 5  
Old 08-19-2014
Hello,

Following may help also for same.

Code:
awk '{match($4,/\-.*/); a=substr($4,RSTART+1,RLENGTH)} {match($4,/.*\-/); b=substr($4,RSTART,RLENGTH-1)} {for(i=1;i<=a;i++){{$4=b"-"i} print $0}}' filename

Output will be as follows.

Code:
chr1 3217769 3217789 2952725-1 255 +
chr1 3217769 3217789 2952725-2 255 +
chr1 3217769 3217789 2952725-3 255 +
chr1 3217769 3217789 2952725-4 255 +
chr1 3217769 3217789 2952725-5 255 +
chr1 3260455 3260475 2434087-1 255 -
chr1 3260455 3260475 2434087-2 255 -
chr1 3260455 3260475 2434087-3 255 -
chr1 3260455 3260475 2434087-4 255 -
chr1 3260455 3260475 2434087-5 255 -
chr1 3260455 3260475 2434087-6 255 -


Thanks,
R. Singh
# 6  
Old 08-19-2014
Another approach:
Code:
awk 'split($2,F," "){p=$0; for(i=1; i<F[1]; i++) {sub(F[1],i,$2); print; $0=p}}1' FS=- OFS=- file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 08-19-2014
Posted by Scrutinizer
Quote:
Another approach:


Code:
awk 'split($2,F," "){p=$0; for(i=1; i<F[1]; i++) {sub(F[1],i,$2); print; $0=p}}1' FS=- OFS=- file
A small change in code as that code will miss one line as it should be <= condition as follows.

Code:
awk 'split($2,F," "){p=$0; for(i=1; i<=F[1]; i++) {sub(F[1],i,$2); print; $0=p}}' FS=- OFS=- filename


Output will be as follows.
Code:
chr1    3217769 3217789 2952725-1       255     +
chr1    3217769 3217789 2952725-2       255     +
chr1    3217769 3217789 2952725-3       255     +
chr1    3217769 3217789 2952725-4       255     +
chr1    3217769 3217789 2952725-5       255     +
chr1    3260455 3260475 2434087-1       255     -
chr1    3260455 3260475 2434087-2       255     -
chr1    3260455 3260475 2434087-3       255     -
chr1    3260455 3260475 2434087-4       255     -
chr1    3260455 3260475 2434087-5       255     -
chr1    3260455 3260475 2434087-6       255     -


EDITED: Sorry my bad Scrutinizer's code is perfect because 1 is there for same.

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-19-2014 at 07:58 AM.. Reason: Added comments about Scrutinizer code is correct
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert rows to column and print output in required format

Hi All, i am trying to print the solaris 11 packages in below required format, But i am unable to do that. Current ouput : root@abc# pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' NAME: QLogic 570x/571x Gigabit Ethernet Driver VERSION: 11.11,REV=2009.11.11 ... (7 Replies)
Discussion started by: balu1234
7 Replies

2. UNIX for Dummies Questions & Answers

Print rows with substring in column

Hi I want to print all rows where there is the alphabet N in the 6th column as a substring. Here is what i tried and not working.Please help ! awk ' { if ( $6 == *"N"* ) print $0} ' awk ' { if ( "${6}" == *N* ) print $0} ' awk ' { if( grep -q N <<<$6) print $0} ' (1 Reply)
Discussion started by: newbie83
1 Replies

3. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Read column from file and delete rows with some condition..

Hi.... I have a need of script to do delete row whenever condition is true.... 2.16 (3) 1 3 9999 0 (1) (0) 34.42 (4) 1 3 9999 37 (2) (3) 34.38 (4) 1 3 9999 64 (2) (3) 34.4 (4) 1 3 1 ... (13 Replies)
Discussion started by: nex_asp
13 Replies

5. Shell Programming and Scripting

Transpose Datefield from rows to column + Print time diff

Hi Experts, Can you please help me in transposing Datefield from rows to column and calculate the time difference for each of the Jobids: Input File: 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012 12:36:26,JOB_5350 Required Output:... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

Print rows in reverse order if values decrease along the column

Hi, Guys. Please help me to find solution to this problem using shell scripting. I have an INPUT file with 4 columns separated by tab. Each block of records is separated by ----- ----- Sample1 5402 6680 Pattern01 Sample2 2216 2368 Pattern02... (6 Replies)
Discussion started by: sam_2921
6 Replies

8. Shell Programming and Scripting

Print rows, having pattern in specific column...

Hello all, :) I have a pattern file some what like this, cd003 cd005 cd007 cd008 and input file like this, abc cd001 cd002 zca bca cd002 cd003 cza cba cd003 cd004 zca bac cd004 cd005 zac cba cd005 cd006 acz acb cd006 cd007 caz cab cd007 ... (25 Replies)
Discussion started by: admax
25 Replies

9. Shell Programming and Scripting

read file and print additional rows till current year

Hi all i have a file like 2006,1,2 2007,2,3 2008,3,4 I will read this and my output should be like 2006,1,2 2007,1,2 2008,1,2 2007,2,3 2008,2,3 2008,3,4 Giving the explanation, we will read the first line of the file and if the year any other than current year, we will print as many... (1 Reply)
Discussion started by: vasuarjula
1 Replies

10. Shell Programming and Scripting

how to read the column and print the values under that column

hi all:b:, how to read the column and print the values under that column ...?? file1 have something like this cat file1 ======= column1, column2,date,column3,column4..... 1, 23 , 12/02/2008,...... 2, 45, 14/05/2008,..... 3, 56, 16/03/2008,..... cat file2 =======... (6 Replies)
Discussion started by: gemini106
6 Replies
Login or Register to Ask a Question