Replacing one Char in a string of variable length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing one Char in a string of variable length
# 8  
Old 07-28-2009
Solution Achieved

danmero,

Your code worked perfectly. Thank you.

Edidataguy, I was looking for the best approach rather than just looking for someone to provide the code, hence why I did not attach any initial code.

I always try to research approch first then code. I was hoping for a reply like, use Sed, or loop through each bit and replace the one you want, then I would have researched that.

I am new to shell, (used to VB) so I was planning to use grep -c to read each char in turn and just write out to a new string until I got to my bit to change etc... This would have taken 6-7 lines of code but sed, did it perfectly.

I now have ordered a sed book to study.

My final code is below. (echo's will be replaced with a -v for production, and -d for write to log eventually)


while read EachLine
do
SFPSBITS=$(echo "$EachLine" |cut -d":" -f2)
SFRECNO=$(echo "$EachLine" |cut -d":" -f3)
SFPHONESTAT=$(echo "$EachLine" |cut -d":" -f4)
echo "PHONESTAT:$SFPHONESTAT"

echo "Bit to change:$SFPSBITS - Record to change:$SFRECNO"
echo "Current PHONESTAT string is $SFPHONESTAT"

SFNEWPHONESTAT=$(echo "$SFPHONESTAT" | sed "s/./B/$SFPSBITS")

echo "New PHONESTAT string:$SFNEWPHONESTAT"
# `set_field $LISTNO PHONESTAT PSBITS -f $SFRECNO -l $SFRECNO`
done < file4.1.$$
# 9  
Old 07-28-2009
Just making sure that people dont come here with there home work and also that they do really try to resolve.
Code:
OIFS=$IFS
IFS=$':'
position=2; xreplwith="B"
# while read str; do
   str='012341231231:2:102939283:NNN:'
   set $xreplwith $position $(echo "$str")
   echo $3:$4:$5:$(echo $6 | sed "s/./$1/$2"):
# done < file
IFS=$OIFS

Please do the following:
1. May be the hard coded values position=2; xreplwith="B" should be replace with params sent to the script as $1 and $2.
2. Remove the "str=" value.
3. Change the "while read" part as required.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. UNIX for Advanced & Expert Users

Replacing string length based on pattern

Hi All, I have a file which is like below. I need to read all the patterns that starts with P and then replace the 9 digit values to 8 digit values (remove leading integer). Can you please help Example : ( Please look below File) File : P,1 M1,... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

3. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Match and variable length string

Hello all, source file looks like this: cat toto.txt NAME) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong OTHEROPT=something ;; NAME2) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong SOMETHING=else ... (5 Replies)
Discussion started by: maverick72
5 Replies

5. Programming

How to find length of string and pass into char array in C?

Hi All I want to take a Hexadecimal number as input and i want to find lenth of the input and pass it to char s ( char s ). I have a program to convert hexadecial to binary but it is taking limited input but i want to return binary number based on input. How? (1 Reply)
Discussion started by: atharalikhan
1 Replies

6. Shell Programming and Scripting

Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with sed -e 's/args=\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'but that wasn't too difficult due to the value being 16 digits. The bank account... (7 Replies)
Discussion started by: mk4mzid
7 Replies

7. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

8. UNIX for Dummies Questions & Answers

string of 7 char length always...

Hi, I know, particular value in the variable should always be of lenth 7 , but the value that is present in thevariable might be of any no.of characters less than or equal to 7... if the no.of characters in the variable is less than 7, I want to add, zeroes at the starting of the field.. How can... (3 Replies)
Discussion started by: thanuman
3 Replies

9. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

10. Shell Programming and Scripting

Finding out the length of a string held within a variable

:confused: Does anyone know which command I can use to find out the length of a string held within a variable? (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question