awk use sequential line numbering in output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk use sequential line numbering in output
# 1  
Old 11-16-2017
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 Smilie.

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

# 2  
Old 11-16-2017
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

This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-17-2017
Thank you very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk changing numbering in output file

The below awk is supposed filter $8 of example.txt using the each line in gene.txt. I think it is but why is it renumbering the 1,2,3 in $1 to 28,29,394? I have attached the data as it is large, example.txt is the file to be searched, gene.txt has the lines to match, and filtered.txt is the current... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Sequential numbering from 1 to ten

Hi I am in a bind, I need create a script that will rename files as they come into a folder with sequential numbering at the begining starting at 1 and proceeding to ten then starting at 1 again. Such as 1_filename.pdf, 2_filename.pdf, 3_filename.pdf, 4_filename.pdf, 5_filename.pdf, 6_filename.pdf,... (6 Replies)
Discussion started by: Paul Walker
6 Replies

3. Shell Programming and Scripting

Inserting new line if two sequential lines begin with the same string

Hi I have a file like this: Peter North Mary Peter North Peter Borough Mary I need there to put 'X' (or anything) on a new line between the two lines where 'Peter' begins the line. There will be many matches to this string, but they will always begin with 'Peter'. Ie, the resulting... (2 Replies)
Discussion started by: majormajormajor
2 Replies

4. Shell Programming and Scripting

start line numbering with arbitrary number?

Hi, I want to do the following: Extract some lines from different files and copy them into one file, with the first column being the line number. I do this with cat file1 file2 file3 |grep 'xxx' |nl > output.file Works fine. But if I want to add data of interest from a fourth file to the... (2 Replies)
Discussion started by: paracetamol
2 Replies

5. Shell Programming and Scripting

sequential to line sequential

Hi I have a file sequential way i.e. written in contineous mode and the Record Seperator is AM from which the record is seperated .Now to process I have to make line sequential,and more over record length is not same it varies as per the input address, AM1234563 John Murray 24 Old streeet old... (5 Replies)
Discussion started by: vakharia Mahesh
5 Replies

6. UNIX for Dummies Questions & Answers

Ghostscript output file numbering?

I am using ghostscript to convert a multi-page pdf file to individual jpg files. I am wondering if there is a way to get ghostscript to start numbering the output jpg files from zero? What i am trying to convey is that it starts naming my files from page_001.jpg, page_002.jpg, etc., and would like... (0 Replies)
Discussion started by: RacerX
0 Replies

7. Shell Programming and Scripting

numbering each line in a text file

I have a simple text file. I want to number each line in that file . for example: My text file is unix my file test My output should be 1 unix 2 my file 3 test (5 Replies)
Discussion started by: pitagi
5 Replies

8. Shell Programming and Scripting

sed or awk help - line numbering w/ different start value

I'm pretty new to sed and awk, and I can't quite figure this one out. I've been trying with sed, as I'm more comfortable with it for the time being, but any tool that fits the bill will be fine. I have a few files, whose contents appear more or less like so: 1|True|12094856|12094856|Test|... (7 Replies)
Discussion started by: camwheel
7 Replies

9. Shell Programming and Scripting

AWK Multi-Line Records Numbering Problem

I have a set of files of multi-line records with the records separated by a blank line. I needed to add a record number to the front of each line followed by a colon and did the following: awk 'BEGIN {FS = "\n"; RS = ""}{for (i=1; i<=NF; i++)print NR,":",$i}' ~/Desktop/data98-1-25.txt >... (3 Replies)
Discussion started by: RacerX
3 Replies

10. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies
Login or Register to Ask a Question