How to put delimiters in text files after fix characters?

Hi ,

i have a text file in which i want to put delimiters after certain characters ( fix),.
like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 .....

files looks like this (original)

22503003673916972040504700538884720121008175657358045042677480191000000919046068826

and i want file like this

225,0,3,003673916972,0405047005388847,20121008175657,3580450426774801,91,000000919046068826,

please suggest any command or script that will do this work?

Thanks in advance

I assume you have a list of delimiter spacings

you can read a string or file one character at a time:

i=0
spacing="3 4 5 17"
while IFS= read -r -n1 char
do
    i=i+1
    echo  -n "$char"
    if [[ "$spacing" =~ "$i" ]]; then 
        echo -n ","
    fi
done < "$file"
echo

Mike

Would something like this work?

$ cat temp.sh
string=2250300367391697204050470053888472012100817
for len in 17 5 4 3; do
  string=`echo $string | sed "s/^\(.\{$len\}\)\(.*\)/\1,\2/"`
done
echo $string

$ ./temp.sh
225,0,3,003673916972,04050470053888472012100817

You can also do this with awk. Note that clist must be initialized to a list of the character positions after which a comma is added (in increasing numeric order):

awk -v clist='3 4 5 17' '
BEGIN { n = split(clist, cl, / /)
}
{       for(i = n; i; i--)
                $0 = substr($0, 1, cl) "," substr($0, cl + 1)
        print
}' input

With the sample input provided, it produces:

225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826

Hi Cragun ,

your code works fine for me..Thanks :-)....( others codes are also working )
I have one more prob...actually this string was from a file , which contains n number of string (around 10K string )
this code works fine for 1 string ...but i need to work that in all my string ..(sample file given below)

cat input.txt
01903003673916970040504600904021820121008175705910040281413617891000000918439013338
01903003673916971040503000655201920121008173606353489041923240491000000919033006189
22503003673916973040503701720296920121008175603354131053633661491000000918983673026
22503003673916972040504700538884720121008175657358045042677480191000000919046068826

can u please help.

Thanks...

while read string; do
  for len in 17 5 4 3; do
    string=`echo $string | sed "s/^\(.\{$len\}\)\(.*\)/\1,\2/"`
  done
  echo $string
  # or the awk code
done < input.txt

To keep your code flexible, i.e. not restricted to a constant number of commas, and running sed just once on the entire file, you may want to try this:

$ POS="3 4 5 17 37 66" 
$ for i in $POS
    do sedcmd="s|.|&,|$i;"$sedcmd
    done
$ echo $sedcmd
s|.|&,|66;s|.|&,|37;s|.|&,|17;s|.|&,|5;s|.|&,|4;s|.|&,|3;

$ sed $sedcmd file
019,0,3,003673916970,04050460090402182012,10081757059100402814136178910,00000918439013338
019,0,3,003673916971,04050300065520192012,10081736063534890419232404910,00000919033006189
225,0,3,003673916973,04050370172029692012,10081756033541310536336614910,00000918983673026
225,0,3,003673916972,04050470053888472012,10081756573580450426774801910,00000919046068826

You may even want to consider to use a variable token:

$ TOK=","
$ for i in $POS; do sedcmd="s|.|&$TOK|$i;"$sedcmd; done

Thanks ,

That works for me.:slight_smile:

I don't understand why you had a problem, with the text above in input.txt, if you change the last line of my script from:

}' input

to:

}' input.txt

the output produced is:

225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826
019,0,3,003673916970,040504600904021820121008175705910040281413617891000000918439013338
019,0,3,003673916971,040503000655201920121008173606353489041923240491000000919033006189
225,0,3,003673916973,040503701720296920121008175603354131053633661491000000918983673026
225,0,3,003673916972,040504700538884720121008175657358045042677480191000000919046068826