Computing.Net > Forums > Unix > insert a delimeter in a text

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

insert a delimeter in a text

Reply to Message Icon

Name: sunilmyneedi
Date: January 23, 2007 at 00:56:47 Pacific
OS: unix
CPU/Ram: none
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: January 23, 2007 at 23:40:18 Pacific
Reply:

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



0

Response Number 2
Name: nails
Date: January 23, 2007 at 23:42:00 Pacific
Reply:

Sorry, shell invocation should be this:

#!/bin/ksh


0

Response Number 3
Name: sunilmyneedi
Date: January 24, 2007 at 05:22:19 Pacific
Reply:

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


0

Response Number 4
Name: nails
Date: January 24, 2007 at 07:03:47 Pacific
Reply:

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.


0

Response Number 5
Name: nails
Date: January 24, 2007 at 15:37:51 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: insert a delimeter in a text

Inserting a string at the end of a www.computing.net/answers/unix/inserting-a-string-at-the-end-of-a-/6205.html

Search for words in a text file www.computing.net/answers/unix/search-for-words-in-a-text-file/7915.html

cancel the enter key in a text www.computing.net/answers/unix/cancel-the-enter-key-in-a-text-/7344.html