Need Help in adding sequence number to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help in adding sequence number to a file
# 1  
Old 01-17-2015
Need Help in adding sequence number to a file

Hi All ,
I have a file which contains data(comma separated) in below format :

Code:
500,Sourav ,kolkata ,8745775020,700091
505,ram,delhi ,9875645874,600032
510 ,madhu ,mumbai ,5698756430 ,500042
515 ,ramesh ,blore ,8769045601 ,400092

I want to add unique sequence number at the start of each line .Output will be like below :

Code:
000001,500,Sourav ,kolkata ,8745775020,700091
000002,505,ram,delhi ,9875645874,600032
000003,510 ,madhu ,mumbai ,5698756430 ,500042
000004,515 ,ramesh ,blore ,8769045601 ,400092

Can anyone tell me please how to do this through unix command .

Last edited by Scrutinizer; 01-18-2015 at 04:08 AM.. Reason: code tags
# 2  
Old 01-17-2015
Code:
awk '{printf("%06d%s%s\n", FNR,OFS,$0) }' OFS=',' infile

Code:
perl -pe '$_ = sprintf("%06d,%s",$.,$_)' infile

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-17-2015
Hello STCET22,

Welcome to forum, kindly use code tags for commands/codes/Inputs in your posts as per forum rules, following may help you in your requirement.
Code:
awk '{printf "%06d,",NR} 1' Input_file

Output will be as follows.
Code:
000001,500,Sourav ,kolkata ,8745775020,700091
000002,505,ram,delhi ,9875645874,600032
000003,510 ,madhu ,mumbai ,5698756430 ,500042
000004,515 ,ramesh ,blore ,8769045601 ,400092

NOTE: Also you can go through the forum rules in following link.
https://www.unix.com/misc.php?do=cfrules


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-17-2015 at 11:58 PM.. Reason: Added forum rules link for user's help
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-18-2015
Hi.

Often one can use standard utilities to handle commonly-occurring issues. In this case:
Code:
nl (1)               - number lines of files

As in this demo:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate line numbering utility nl

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C nl

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
nl -v1 -i1 -l1 -s, -w6 -nrz -hn -bt -fn $FILE

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
Linux 2.6.26-2-amd64
GNU bash 3.2.39
nl (GNU coreutils) 6.10

-----
 Input data file data1:
500,Sourav ,kolkata ,8745775020,700091
505,ram,delhi ,9875645874,600032
510 ,madhu ,mumbai ,5698756430 ,500042
515 ,ramesh ,blore ,8769045601 ,400092

-----
 Results:
000001,500,Sourav ,kolkata ,8745775020,700091
000002,505,ram,delhi ,9875645874,600032
000003,510 ,madhu ,mumbai ,5698756430 ,500042
000004,515 ,ramesh ,blore ,8769045601 ,400092

See man nl for details.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 01-18-2015
Nice drl, I always use cat -n but it's much less configurable.

Many of those options are the defaults and I believe the OP could simply use:

Code:
nl -w6 -nrz -s, infile

# 6  
Old 01-18-2015
I like the following, it is one of the few exceptions to the rule of thumb that sed ... | sed ... is a bad idea. "<t>" is a literal tab character.

Code:
sed = /path/to/file | sed 'N;s/\n/<t>/'

I hope this helps.

bakunin
# 7  
Old 01-19-2015
Need explanation

Hi Ravinder,

Thanks for your help .

Could you please explain the below block of code.


awk '{printf "%06d,",NR} 1' Input_file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check the missing file based on sequence number.

Hi All, I have a requirement that i need to list only the missing sequences with a unix script. For Example: Input: FILE_001.txt FILE_002.txt FILE_005.txt FILE_006.txt FILE_008.txt FILE_009.txt FILE_010.txt FILE_014.txt Output: FILE_003.txt FILE_004.txt FILE_007.txt FILE_011.txt... (5 Replies)
Discussion started by: Arun1992
5 Replies

2. Shell Programming and Scripting

Renaming a file with sequence number

Hi team, I need a script for renaming a file with sequence number. script get a file from one directory, /home/billing/Cmm/sms/sms_tmp, append sequence no at the end of file name and move a file to other directory, /home/billing/Cmm/sms/. Actual file is cdr201508271527, and file after... (10 Replies)
Discussion started by: mfaizan40
10 Replies

3. Shell Programming and Scripting

Appending sequence number at the end of file name

hi team, i need a script for renaming a file with sequence number. script get a file from one directory'/home/billing/Cmm/sms/sms_tmp' append sequence no at the end of file name and move a file to other directory/home/billing/Cmm/sms/. actual file is cdr201508271527 file after renaming ... (3 Replies)
Discussion started by: mfaizan40
3 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. UNIX for Dummies Questions & Answers

Adding a running sequence number while appending 2 files

Hi, I have to append 2 files and while appending i need to add a running sequence number (counter ) for each line at the first column. For e.g. If file x contains details as below. Tom Dick Harry Charlie and file y contains Boston Newyork LA Toledo Then the new file should... (1 Reply)
Discussion started by: kalyansid
1 Replies

6. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

7. Shell Programming and Scripting

Adding sequence to the file

How do I add the sequence number to the file? I have a file seperated by commas. appusage,243,jsdgh,798 appusage,876,0989,900 . . appusage,82374,ajfgdh,9284 The output would be as below 1,appusage,243,jsdgh,798 2,appusage,876,0989,900 . . 100,appusage,876,0989,900 (5 Replies)
Discussion started by: smee
5 Replies

8. Shell Programming and Scripting

How to split a file with adding sequence number and extension.

I have a file name -HRCFTSIN05PLA1602100430444444 my requirement is to split the file in 10000 count each file and to add sequence number.rch at the end of each file. output should be in this format HRCFTSIN05PLA160210043044444401.rch HRCFTSIN05PLA160210043044444402.rch... (4 Replies)
Discussion started by: abhigrkist
4 Replies

9. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies

10. Shell Programming and Scripting

Adding a sequence string to a file

I have a pipe delimited file I need to add a sequence number to in the third field. The record fields will be variable length, so I have to parse for the second pipe. Another requirement is that the sequence number must be unique to all records in the file and subsequent files created, so the... (5 Replies)
Discussion started by: MrPeabody
5 Replies
Login or Register to Ask a Question