The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 05:29 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 06:19 AM
generate random number in perl zx1106 Shell Programming and Scripting 2 03-17-2008 08:13 PM
generate random number in korn shell frustrated1 Shell Programming and Scripting 2 12-31-2005 03:49 AM
How to generate a random number? MacMonster High Level Programming 2 10-15-2001 09:35 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: May 2008
Posts: 8
Stumble this Post!
Exclamation 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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: Jun 2008
Location: Redmond, WA
Posts: 11
Stumble this Post!
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
Reply With Quote
  #3 (permalink)  
Old 06-11-2008
Registered User
 

Join Date: May 2008
Posts: 8
Stumble this Post!
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0