Give name for every nth line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Give name for every nth line
# 1  
Old 11-01-2012
Give name for every nth line

Hi all,

Greetings. I am facing some troubles and hope that the professionals here could help me.

I am handling a large dataset, and part of it is like below:

Code:
1	ab139	0	752566	G	A
1	ab151	0	846808	T	C
1	ab142	0	854250	G	A
1	ab061	0	861808	A	G
1	ab043	0	873558	T	G
1	ab034	0	882033	A	G
1	ab395	0	888659	T	C
1	ab608	0	891945	A	G
1	ab100	0	894573	G	A
1	ab109	0	903104	T	C
1	ab981	0	904165	A	G
1	ab682	0	918384	G	T
2	ab293	0	112433668	T	C
2	ab204	0	112435483	C	T
2	ab331	0	112437140	A	G
2	ab691	0	112444459	T	G
2	ab104	0	121202941	C	T
2	ab107	0	121203511	T	C
2	ab029	0	121214402	T	C
2	ab988	0	121214469	T	G
2	ab666	0	121216828	T	G
2	ab422	0	121217568	A	G
3	ab555	0	63754993	G	T
3	ab443	0	63779288	C	T
3	ab332	0	63785436	C	T
3	ab991	0	63788331	G	A
3	ab111	0	63811369	A	C
3	ab698	0	63824453	T	C
3	ab327	0	63838796	G	A
3	ab570	0	63842062	A	G
3	ab110	0	72360478	T	C
3	ab991	0	72361951	G	A
3	ab867	0	72382471	A	G
3	ab117	0	72386350	A	G
3	ab101	0	72387348	G	A
3	ab871	0	72387926	T	C

How can I add an indicator after 5 lines like the output below?
Code:
1	ab139	0	752566	G	A	Fr1.1
1	ab151	0	846808	T	C	Fr1.1
1	ab142	0	854250	G	A	Fr1.1
1	ab061	0	861808	A	G	Fr1.1
1	ab043	0	873558	T	G	Fr1.1
1	ab034	0	882033	A	G	Fr1.2
1	ab395	0	888659	T	C	Fr1.2
1	ab608	0	891945	A	G	Fr1.2
1	ab100	0	894573	G	A	Fr1.2
1	ab109	0	903104	T	C	Fr1.2
1	ab981	0	904165	A	G	Fr1.3
1	ab682	0	918384	G	T	Fr1.3
2	ab293	0	112433668	T	C	Fr2.1
2	ab204	0	112435483	C	T	Fr2.1
2	ab331	0	112437140	A	G	Fr2.1
2	ab691	0	112444459	T	G	Fr2.1
2	ab104	0	121202941	C	T	Fr2.1
2	ab107	0	121203511	T	C	F2.2
2	ab029	0	121214402	T	C	F2.2
2	ab988	0	121214469	T	G	F2.2
2	ab666	0	121216828	T	G	F2.2
2	ab422	0	121217568	A	G	F2.2
3	ab555	0	63754993	G	T	Fr3.1
3	ab443	0	63779288	C	T	Fr3.1
3	ab332	0	63785436	C	T	Fr3.1
3	ab991	0	63788331	G	A	Fr3.1
3	ab111	0	63811369	A	C	Fr3.1
3	ab698	0	63824453	T	C	Fr3.2
3	ab327	0	63838796	G	A	Fr3.2
3	ab570	0	63842062	A	G	Fr3.2
3	ab110	0	72360478	T	C	Fr3.2
3	ab991	0	72361951	G	A	Fr3.2
3	ab867	0	72382471	A	G	Fr3.3
3	ab117	0	72386350	A	G	Fr3.3
3	ab101	0	72387348	G	A	Fr3.3
3	ab871	0	72387926	T	C	Fr3.3

Can sed/awk do this? Please help me, your comments are very much appreciated. Thank you very much for your kind helps in advance.

Best wishes,
Amanda
(Is x86_64 GNU/Linux)
# 2  
Old 11-01-2012
Try


Code:
awk '{a++;if(!(a%5) || NR==1){s++}
    {if(P != $1){s=1;a=0}}
    {P=$1;$(NF+1)="Fr"$1"."s}}1' file


Last edited by pamu; 11-01-2012 at 03:09 AM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 11-01-2012
Code:
awk -F'\t' 'p1!=$1{c=1;n=1}
{$(NF+1)= "Fr" $1 "." n}
!(c%5){n++}
{c++;p1=$1}1' OFS='\t' file


Last edited by elixir_sinari; 11-01-2012 at 03:15 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 11-01-2012
Dear pamu and elixir sinari,

Thank you very much! It works!! Really helps a lot.

Best wishes,
Amanda
# 5  
Old 11-03-2012
knowing it's a bit late, I couldn't resist:
Code:
awk     '$1!=sav {sav=$1;n=i=0}
          {n+=!(i++%5); print $0,"Fr"$1"."n}
    ' FS="\t" OFS="\t" file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

Print nth line in a file

Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Can I use grep in this case ? Example: In the below file, 12th line is "Kernel parameter check passed for rmem_max" . I just want the 12 line to be printed. # cat sometext.txt Kernel version check... (2 Replies)
Discussion started by: John K
2 Replies

4. Shell Programming and Scripting

Print every nth line

Dear all, How to print every nth line. File like this: File input: 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 (3 Replies)
Discussion started by: attila
3 Replies

5. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. UNIX for Dummies Questions & Answers

how to delete nth line

can you please tell me (1) how to delete 1st and 3rd line only from a file. (2) How to delete last 4 lines in a file that has 2 blank lines out of last 4 lines. Thank you. (2 Replies)
Discussion started by: Ariean
2 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies

9. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

10. Shell Programming and Scripting

Get Nth record from last line

I have a growing file . I knew only last few lines which is constant for evey job run. I'd need to pull the Nth record from the last line. In this case i should not use search pattern. (2 Replies)
Discussion started by: ford2020
2 Replies
Login or Register to Ask a Question