insert a delimeter in a text
|
Original Message
|
Name: sunilmyneedi
Date: January 23, 2007 at 00:56:47 Pacific
Subject: insert a delimeter in a textOS: unixCPU/Ram: noneModel/Manufacturer: none |
Comment: hi i would like to insert a delimiter in a text at specified location. for example : initial text : thisisanexample desired output: this|is|an|example could any one help me ... thanx
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: January 23, 2007 at 23:40:18 Pacific
|
Reply: (edit)It's not very pretty or elegant: Cut the string a character at a time, append each character to a new string, and append the delimiter at the proper postion: !/bin/ksh mystring="thisisanexample" newstring="" count=0 length=$(expr "$mystring" : '.*') until [ $count -eq $length ] do ((count+=1)) char=$(echo $mystring|cut -b"$count") newstring=${newstring}${char} # place delimiter at position 4,6,8 if [[ $count -eq 4 || $count -eq 6 || $count -eq 8 ]] then newstring=${newstring}"|" fi done echo $newstring
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: sunilmyneedi
Date: January 24, 2007 at 05:22:19 Pacific
|
Reply: (edit)thanx .. but my string is too big and i have around 30 delimiter i have to use. my ultimate intension is to make the text file divided into columns. so my text file contains thousands of lines and for each line doing as above is not worth i believe .. can you please suggest me any other alternative to preform the same...
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: nails
Date: January 24, 2007 at 07:03:47 Pacific
|
Reply: (edit)Is there a pattern to where you place the delimiters? For example, insert a delimiter at position 4 and then insert a delimiter every 2 characters. There are sed examples that insert commas into large decimal number strings. If your delimiter location is random, then I don't see any better solution.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: nails
Date: January 24, 2007 at 15:37:51 Pacific
|
Reply: (edit)This is an interesting problem. I don't think there is a unix tool that will easily solve your problem. Since you said that your number of delimiters could up to be 30, I placed the delimiters into a string (although it could easily be a file). For each delimiter position, I create a dynmaic variable. Here's another copy of the code wtih your "thisisanexample" string placed in a file: #!/bin/ksh # place variable in myfile #mystring="thisisanexample" mydelim="|" # place the delimiter positions in a variable myposition="4 6 8" # create dynamic variables for each position - 3 in this case indcnt=0 for i in $(echo ${myposition}) do ((indcnt+=1)) eval a$indcnt=\$i done while read mystring do newstring="" count=0 length=$(expr "$mystring" : '.*') until [ $count -eq $length ] do ((count+=1)) char=$(echo $mystring|cut -b"$count") newstring=${newstring}${char} ncnt=0 # cycle thru the dynamic variables until [ $ncnt -eq $indcnt ] do ((ncnt+=1)) val=$(eval echo \"\$a$ncnt\") if [[ $count -eq $val ]] then newstring=${newstring}${mydelim} # stop if one is found break fi done done echo $newstring done < myfile
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: