Need Help in adding sequence number to a file

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

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 :

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 .

awk '{printf("%06d%s%s\n", FNR,OFS,$0) }' OFS=',' infile
perl -pe '$_ = sprintf("%06d,%s",$.,$_)' infile
1 Like

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.

awk '{printf "%06d,",NR} 1' Input_file

Output will be as follows.

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.

Thanks,
R. Singh

1 Like

Hi.

Often one can use standard utilities to handle commonly-occurring issues. In this case:

nl (1)               - number lines of files

As in this demo:

#!/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:

$ ./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

1 Like

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:

nl -w6 -nrz -s, infile

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.

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

I hope this helps.

bakunin

Hi Ravinder,

Thanks for your help .

Could you please explain the below block of code.

awk '{printf "%06d,",NR} 1' Input_file

Hello STCET22,

Following explaination may help you in same.

awk '{printf "%06d,",NR} 1' Input_file

printf is to print the values, %06d it specifies the minimum number of digits to print(as we want to print 5 zeros with value of Line number so it will print 5 zeros and then line number), NR The total number of input records seen so far or line number, at last as we know awk works as condition { action statements } so I have given here 1 which indicates that condition should be true and if no action is given then by default action which is print will happen, so it will print complete line value(already it has printed line number with 5 zeros then it will print the actual line content.). Then it will read next line and so on, there is one more thing here called ORS means Output record seperator if didn't specify it by default it will be \n(new line) so when it prints complete line it will print ORS next line also.

Thanks,
R. Singh