Visit Our UNIX and Linux User Community


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
# 1  
Old 07-27-2009
Replacing one Char in a string of variable length

Hi all,

I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters.

I am reading a line in from a file which contains
012341231231:2:102939283:NNN: Require :NBN:
012838238232:3:372932:NNN: Require :NNB:
I need to change 1 N or a B depending on the number in the 2nd field (a 2 or a 3 in the cases above).

Now each character in the NNN field could initially be N,C,B, or 2 or 3 other characters.

At the moment I can only think of looping around each character using a cut -c to write out each character as it already is then changing the one I want when I get to it, but I was hoping there could be a better and more efficient way.

I am using Redhat Ent 4 with ksh

Thanks in advance

Nigel...
# 2  
Old 07-27-2009
I am sorry but I am not able to understand what you want.
# 3  
Old 07-27-2009
what have you tried??
use awk..
# 4  
Old 07-27-2009
Anchal_khare,

Sorry I'll explain a little better.

I have a string "NNNCCCXXXX" for example. I will need to change one of these characters to B so for example if I needed to change Char 3 I would want to have "NNBCCCXXXX" I will know the char I need to change and the current string.

To answer Vidyadhar85's questions. I have not tried awk, as I have never used it. Just trying to gain best approach then I'll start researching and coding.
# 5  
Old 07-27-2009
Quote:
Originally Posted by nkwilliams
I have a string "NNNCCCXXXX" for example. I will need to change one of these characters to B so for example if I needed to change Char 3 I would want to have "NNBCCCXXXX" I will know the char I need to change and the current string.
Code:
# X=3;
# echo "NNNCCCXXXX" | sed "s/N/B/$X"
NNBCCCXXXX

# 6  
Old 07-27-2009
That would change the 3rd N to a B in the string not necessarily the 3rd character to a B. Not sure what you want exactly but you might try:

Code:
# X=3
# echo "NNNCCCXXXX" | sed "s/./B/$X"
NNBCCCXXXX

# 7  
Old 07-27-2009
What have you tried so far?
Post that code first.

Post atleast 10 lines of input and the expected output.
The more info you give the shorter the program you get.

My code converts this:
Code:
012341231231:1:102939283:XYZ:
012341231231:2:102939283:GGGGG:
012341231231:3:102939283:--------:

Into this:
Code:
012341231231:1:102939283:BYZ:
012341231231:2:102939283:GBGGG:
012341231231:3:102939283:--B----:

Is this what you want?

Last edited by edidataguy; 07-27-2009 at 09:08 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #726
Difficulty: Medium
Satoshi Yamashita is a pseudonym for the anonymous creator or creators of Bitcoin.
True or False?

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

Featured Tech Videos