Need append sequence number

Hi,

Need to add sequnce number to one of the csv file and please find below
actual requirement.

Input file

ABC,500
XXQ,700
ADF,400,
ART,200

Out put file should be

1,ABC,500
2,XXQ,700
3,ADF,400,
4,ART,200

Just need to add sequence number for every record and please share nix command how to generate sequence values.
Thanks for your help.

Try:

awk '{print NR, $0}' OFS=, file
1 Like

Thanks for your the update and this works for me and is there any other command I can use which is update directly inside the file rather than redirecting writing to other file.

Hello siva83,

There is a sed command with -i option.
But it is not always safe so you can use following in spite of that.

cat file.ksh
awk '{print NR, $0}' OFS=, Input_file > TEMP_FILE
if [[ -f TEMP_FILE ]]
then
       mv TEMP_FILE Input_file
else
       echo "Please check TEMP file has NOT been created."
fi

Here I am taking output to a temp file which I am later renaming as the Input_file by mv command. Hope this helps.

Thanks,
R. Singh