I have a file with a 1000 ids in the form of strings. I want to replace each id with a unique numbers in the whole file. each id is repeating in all the columns. I know I can use sed command but there are many ids in file which are need to be converted
You could prepare a translation table in a second file and let a script read this file and invoke sed on every line. For instance (just a sketch):
The translation file:
0001=B752
0002=B295
0003=B289
...
The script:
#! /bin/ksh
chCode=""
chID=""
fTranslationTable="/path/to/some/file.xlate"
fWork="/path/to/your/file"
cat $fTranslationTable | cut -d'=' -f1,2 | while read chCode chID ; do
sed "/${chID}/${chCode}/g" ${fWork} > ${fWork}.tmp
mv ${fWork}.tmp ${fWork}
done
If the codes you want to replace the IDs with are only required to be unique you could even create them dynamically by splitting the lines of your original file so that every ID sits on a single line. sort -u will give you a list of unique IDs then and you can automatically create a code for every one of them, getting the translation table i used above. From there on you could use my method to replace the IDs with these codes.