Help with replacing a char

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with replacing a char
# 1  
Old 08-25-2017
Help with replacing a char

Hello All,

I have a file as below . I want to convert the Y with numbers to H

From
Code:
4,
M11,
P2521759,
Y75,Y70,Y105,Y110,Y700,Y815,Y830,Y900,Y162,Y300,
Y291,Y290,Y15,Y20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

to
Code:
4,
M11,
P2521759,
H75,H70,H105,H110,H700,H815,H830,H900,H162,H300,
H291,H290,H15,H20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

I tried to do
Code:
sed -i 's/Y/H/g' file

it converted my Yesterday to Hesterday I want to convert only when there is Y followed by integer

Thanks
Moderator's Comments:
Mod Comment Due to repeated failures to properly format posts, this account has been placed in read only mode for a day.

Last edited by Don Cragun; 08-26-2017 at 02:01 AM.. Reason: Change QUOTE tags to CODE tags; remove unneeded blank lines, fix spacing mourned ICODE tags.
# 2  
Old 08-26-2017
Quote:
Originally Posted by arunkumar_mca
Hello All,

I have a file as below . I want to convert the Y with numbers to H

From
Code:
4,
M11,
P2521759,
Y75,Y70,Y105,Y110,Y700,Y815,Y830,Y900,Y162,Y300,
Y291,Y290,Y15,Y20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

to
Code:
4,
M11,
P2521759,
H75,H70,H105,H110,H700,H815,H830,H900,H162,H300,
H291,H290,H15,H20,
MR2716014,MR2617014,
Yesterday,current
1,201012,
102032,1
11112,0

I tried to do
Code:
sed -i 's/Y/H/g' file

it converted my Yesterday to Hesterday I want to convert only when there is Y followed by integer

Thanks
Moderator's Comments:
Mod Comment Due to repeated failures to properly format posts, this account has been placed in read only mode for a day.
As you well know, it always helps to know what operating system and shell you're using when you ask questions in this forum. Assuming that the sed on your system conforms to POSIX Standard requirements (which is not true by default on Linux systems), you could try:
Code:
sed 's/Y\([0-9]\)/H\1/g' file

If this doesn't work on your system, please tell us what operating system you're using (including the version number).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-26-2017
Hello arunkumar_mca,

Could you please try following too and let me know if this helps you.
Code:
awk -F, '{for(i=1;i<=NF;i++){if($i ~ /Y[0-9]+/){sub(/Y/,"H",$i)}}} 1' OFS=,   Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing multiple special chars with single char

Hi I've a string . And i need to replace set of characters with a single character Means .. or . or ... and so on should be replaced with single % character Irrespective of number of dots in between the characters , those should be replaced with single % All the above strings should be... (3 Replies)
Discussion started by: smile689
3 Replies

2. UNIX for Dummies Questions & Answers

Replacing char in filename scripts fails

Hi I'm trying to remove what I "think" is a bad character. How I got the bad character is when I downloaded jpgs onto my PC and then renamed the files using windows explorer. In cygwin, the files look like $ dir -l total 7840 ----------+ 1 None 3647968 Jul 21 08:41 2012-07-21\ (1).JPG... (6 Replies)
Discussion started by: SailingDreams
6 Replies

3. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

4. Shell Programming and Scripting

Reading a file and replacing char by position

Hi I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting. (4 Replies)
Discussion started by: macastor
4 Replies

5. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: nkwilliams
8 Replies

6. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

7. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

8. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

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

10. UNIX for Dummies Questions & Answers

replacing all 4 first upper char of every rec to lowercase ?

I have a file where some records have been updated the wrong way and need to fix it quickly since the amount can be alot. Every record where any of the first 4 characters are in upper case need to be changed to lowercase. Records can have '#' in position-1 for comments. These musn't be... (2 Replies)
Discussion started by: Browser_ice
2 Replies
Login or Register to Ask a Question