Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk use sequential line numbering in output

Shell Programming and Scripting


Tags
awk, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-16-2017   -   Original Discussion by cmccabe
cmccabe's Unix or Linux Image
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 15 February 2018, 1:54 PM EST
Location: Chicago
Posts: 1,212
Thanks: 729
Thanked 14 Times in 13 Posts
awk use sequential line numbering in output

The awk below produces an output with the original header and only the matching lines (which is good), but the output where the original line numbering in the match found on is used. I can not figure out how to sequentially number the output instead of using the original.

I did try to add {$1=++n; print} before the FNR==1 and though it executed the output was not correct. Thank you Linux.

file1


Code:
LYST	NM_000081.3	chr1	235972867	235972867
MSH6	NM_000179.2	chr2	48027375	48027375

file2


Code:
R_Index	Chr	Start	End	Ref	Alt	Func.refGeneWithVer	Gene.refGeneWithVer
1362	chr1	235972867	235972867	T	C	exonic	LYST
5	chr1	985239	985239	C	T	splicing	AGRN
1701	chr2	48027375	48027375	T	C	exonic	MSH6


awk


Code:
awk -F'\t' 'NR==FNR{c[$1$3$4]++;next};c[$8$2$3] > 0; FNR==1' file1 file2

current output


Code:
R_Index	Chr	Start	End	Ref	Alt	Func.refGeneWithVer	Gene.refGeneWithVer
1362	chr1	235972867	235972867	T	C	exonic	LYST
1701	chr2	48027375	48027375	T	C	exonic	MSH6

desired output


Code:
R_Index	Chr	Start	End	Ref	Alt	Func.refGeneWithVer	Gene.refGeneWithVer
1	chr1	235972867	235972867	T	C	exonic	LYST
2	chr2	48027375	48027375	T	C	exonic	MSH6

Sponsored Links
    #2  
Old Unix and Linux 11-16-2017   -   Original Discussion by cmccabe
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 18 February 2018, 7:08 PM EST
Location: Galactic Empire
Posts: 3,661
Thanks: 257
Thanked 1,327 Times in 1,244 Posts


Code:
awk -F'\t' 'NR==FNR{c[$1$3$4]++;next}c[$8$2$3]>0{$1=++n;print}FNR==1' OFS='\t' file1 file2

The Following User Says Thank You to Yoda For This Useful Post:
cmccabe (11-17-2017)
Sponsored Links
    #3  
Old Unix and Linux 11-17-2017   -   Original Discussion by cmccabe
cmccabe's Unix or Linux Image
cmccabe cmccabe is offline
Registered User
 
Join Date: Nov 2013
Last Activity: 15 February 2018, 1:54 PM EST
Location: Chicago
Posts: 1,212
Thanks: 729
Thanked 14 Times in 13 Posts
Thank you very much Linux.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk changing numbering in output file cmccabe Shell Programming and Scripting 2 08-18-2016 10:21 AM
Sequential numbering from 1 to ten Paul Walker Shell Programming and Scripting 6 02-25-2014 01:07 PM
sequential to line sequential vakharia Mahesh Shell Programming and Scripting 5 02-04-2011 12:02 AM
Ghostscript output file numbering? RacerX UNIX for Dummies Questions & Answers 0 03-15-2010 06:23 PM
Reading special characters while converting sequential file to line sequential Rajeshsu Programming 2 07-10-2006 03:38 PM



All times are GMT -4. The time now is 05:47 AM.