AWK: generate new line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: generate new line number
# 1  
Old 06-11-2008
Error AWK: generate new line number

Hi.
I have a script wich reads 1 file and generates 4. If the original file has 10 lines the the sum of the 4 generated files must have the 10 original lines. So far this works.

Now what I need is to numerate the lines wtithin each generated file.
I tried with NR but it prints the line number of the original file, and I need each file to start from 1.

Example:
Original: 1,2,3,4,5,6,7,8,9
using NR Gen_1: 2,5,8,9
Gen_2: 1,3,4,6,7

and what I need is:
Original: 1,2,3,4,5,6,7,8,9
Gen_1: 1,2,3,4
Gen_2: 1,2,3,4,5

any ideas?

thanks in advance.
# 2  
Old 06-11-2008
After you have created your 4 files, you could do something like

awk "{print NR,$0}" file1 > file1_with_line_numbers
awk "{print NR,$0}" file2 > file2_with_line_numbers
awk "{print NR,$0}" file3 > file3_with_line_numbers
awk "{print NR,$0}" file4 > file5_with_line_numbers
# 3  
Old 06-11-2008
Yes, that could work.
However the whole idea of the script is to read the source file and write the generated files only once.
Thanks for the idea, but given the size of the files to process, the line number should be writen with the rest of the line, so the file only gets processed once.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

awk for line number 2

This is what I have so far. xrandr | grep connected | grep -v disconnected | awk '{print $1}' This is my output. LVDS1 TV1 How can I awk for line number 2. The only output I want is TV1. (11 Replies)
Discussion started by: cokedude
11 Replies

3. Shell Programming and Scripting

Help on Sed/awk/getting line number from file

I Have file1 with below lines : #HostNameSelection=0 :NotUsed #HostNameSelection=1 :Automatic #HostNameSelection=3 :NotForced I have file2 which has similar lines but with different values I want to copy the changes from file1 to file2 ,line by line only if line begins with '#'. for... (7 Replies)
Discussion started by: mvr
7 Replies

4. Shell Programming and Scripting

help: Awk to control number of characters per line

Hello all, I have the following problem: My input is two sorted files: file1 >1_19_130_F3 T01220131330230213311013000000110000 >1_23_69_F3 T01200211300200200010000001000000 >1_24_124_F3 T010203113002002111111200002010 file2 >1_19_130_F3 24 18 9 18 23 4 11 4 5 9 5 8 15 20 4 4 7 4... (9 Replies)
Discussion started by: DerSeb
9 Replies

5. Programming

Generate random number

I saw this formula to generate random number between two specified values in shell script.the following. $(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Give a example in book. Generate random number between 6 and 30.like this. $(((RANDOM%30/3+1)*3)) But I have a... (1 Reply)
Discussion started by: luoluo
1 Replies

6. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

7. Shell Programming and Scripting

awk help,line number

I am grep-ing the word "this" in all the files in my dir. $ awk '/this/' * this is this this I want the output as: 1)this is 2)this 3)this How can I achieve this ? Please help. HTH, jkl_jkl (4 Replies)
Discussion started by: jkl_jkl
4 Replies

8. Shell Programming and Scripting

printing a line number using awk

Hi Chaps, I'm trying to print the line number of a comma delimited file where the second field in the line is blank using AWK. Here is the code I have so far where am I going wrong. It is the last column in the file. nawk -v x==0 'BEGIN {FS=",";OFS=","} x++ if ($2 == " ") print $x' bob.tst ... (3 Replies)
Discussion started by: rjsha1
3 Replies

9. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies

10. Shell Programming and Scripting

Getting line number while using AWK

Using AWK, while I am reading the file, I am separating fields based on the ':' & using NF. I also would like to mention line numbers from the file they are originally from. How would I take out the line number for them? I am trying something like following , awk -F":" '{ j=1 for (i=1;... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question