The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to uniq third field in a file babycakes UNIX for Dummies Questions & Answers 1 01-31-2008 11:52 PM
compare two col from 2 files, and output uniq from file 1 pp56825 Shell Programming and Scripting 2 01-10-2008 07:10 AM
Inserting argument into top of a file Dev06 UNIX for Dummies Questions & Answers 7 10-05-2006 01:16 PM
Inserting a new column in a file rosh0623 UNIX for Advanced & Expert Users 4 08-01-2006 08:15 AM
Reading special characters while converting sequential file to line sequential Rajeshsu High Level Programming 2 07-10-2006 11:38 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-07-2005
Registered User
 

Join Date: Mar 2005
Posts: 47
inserting uniq sequential numbers at the start of the file

Hi Unix gurus,
I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number.


Example:

File is as follows

|123|4test|test
|121|2test|test
|x12|1test|test
|vd123|5test|test

starting number is : 120

output must be :

120|123|4test|test
121|121|2test|test
122|x12|1test|test
123|vd123|5test|test


Advance thanks for helping.

Jingi
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-07-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
awk 'BEGIN{n=120}{printf("%s%s\n",n++,$0)}' infile > outfile

Cheers
ZB
Reply With Quote
  #3 (permalink)  
Old 11-08-2005
Registered User
 

Join Date: Mar 2005
Posts: 47
Quote:
Originally Posted by zazzybob
awk 'BEGIN{n=120}{printf("%s%s\n",n++,$0)}' infile > outfile

Cheers
ZB

Can 120 be a varaible? I mean

variable=12

awk 'BEGIN{n=$variable}{printf("%s%s\n",n++,$0)}' infile > outfile

??

The aboe statement doesn't work if it is variable..what is the work around?
Reply With Quote
  #4 (permalink)  
Old 11-08-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by jingi1234
Can 120 be a varaible? I mean

variable=12

awk 'BEGIN{n=$variable}{printf("%s%s\n",n++,$0)}' infile > outfile

??

The aboe statement doesn't work if it is variable..what is the work around?
Code:
nawk -v n=120 '{printf("%s%s\n",n++,$0)}' infile > outfile
Reply With Quote
  #5 (permalink)  
Old 11-08-2005
Registered User
 

Join Date: Mar 2005
Posts: 47
Unhappy

What if the file has "tab" [\t] as a delimter? what should i change in the nawk?

Please help

Last edited by jingi1234; 11-17-2005 at 08:22 AM.
Reply With Quote
  #6 (permalink)  
Old 11-17-2005
Registered User
 

Join Date: Mar 2005
Posts: 47
Gurus,
Please help
Reply With Quote
  #7 (permalink)  
Old 11-19-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by jingi1234
What if the file has "tab" [\t] as a delimter? what should i change in the nawk?

Please help
'man nawk' should help.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:17 PM.


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