How to replace characters in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace characters in a file?
# 1  
Old 06-24-2013
How to replace characters in a file?

Hi Gurus,

I need replace some charactors in a file.
in following example. I need replace from 4th charactor to 6th charactor with x in each line.
Code:
abcdefghijklmn
123456789011

excepted result:
Code:
abcxxxghijklmn
123xxx789011

Thanks in advance.
# 2  
Old 06-24-2013
Using awk substr function:
Code:
awk '{print substr($0,1,3)"xxx"substr($0,7)}' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-24-2013
Quote:
Originally Posted by Yoda
Using awk substr function:
Code:
awk '{print substr($0,1,3)"xxx"substr($0,7)}' file

It works great. Thanks

SmilieSmilieSmilie
# 4  
Old 06-25-2013
With sed:
Code:
sed 's/.../xxx/2' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-25-2013
Substitute after 3 characters
Code:
sed 's/\(.\{3\}\).../\1xxx/' file

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 06-26-2013
Thanks Guys,
All suggestions are work fine.
I have one more quesiton, if I have 50 or more charactor's need to be replaced, if there any easy way to avoid to type 50 'X'?

Thanks in advance
# 7  
Old 06-26-2013
Try:
Code:
awk '{for(i=s+1; i<=w+s; i++) $0=substr($0,1,i-1) "x" substr($0,i+1)}1' s=3 w=50 file

or
Code:
awk -v s=3 -v w=50 'BEGIN{r=sprintf("%0" w "s",x); gsub(0,"x",r)} {print substr($0,1,s) r substr($0,s+w+1)}' file

gawk and some other awks:
Code:
awk '{for(i=s+1; i<=w+s; i++) sub(/./,"x",$i)}1' s=3 w=50 FS= OFS= file

gawk only:
Code:
gawk -v s=3 -v w=50 'BEGIN{FIELDWIDTHS=s " " w " " 1000} {gsub(/./,"x",$2)}1' OFS= file

with bash, ksh93:
Code:
xout=$(printf "%050s" ""); sed "s/\(...\).\{50\}/\1${xout//0/x}/" file


Last edited by Scrutinizer; 06-26-2013 at 02:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to replace new line characters in a huge file

Hi , I would like to replace new line characters(\n) in a huge file of about 2 million records . I tried this one (:%s/\n//g) but it's hanging there and no result. Does this command do not work if the file is big. Please let me know if you have any other options Regards Raj (1 Reply)
Discussion started by: rajeevm
1 Replies

2. Shell Programming and Scripting

Replace first 3 characters in a unix file in all lines

Replace first 3 characters in a unix file (say replace "A&B" with "C&D") in all lines of the file. Need a sed or awk script to do this. Kindly help! -Kumar (4 Replies)
Discussion started by: vasan2815
4 Replies

3. Shell Programming and Scripting

Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question. I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes,... (7 Replies)
Discussion started by: SomeTechGuy
7 Replies

4. Shell Programming and Scripting

Replace 10 characters in file

Hi, I need to replace 10 characters string (21-30) in a file with another string. I tried using cut command, i am able get these 10 charaters, but do not know how to replace them inside the file. for example file content(these are alphanumeric characters):... (3 Replies)
Discussion started by: Johny001
3 Replies

5. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies

6. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

7. Shell Programming and Scripting

replace characters in a file

Hi, I have a file in which i want to replace the charaters from position 3-5 with a particular string for the first line. For ex The file contains abcdefghij jkdsflsfkdk 908090900 i want to replace the characters 3-5 for the first line as 678 so, the file should look like ... (7 Replies)
Discussion started by: dnat
7 Replies

8. Shell Programming and Scripting

How to replace characters 7 through 14 of every line in a file

Hi all, I have a file with multiple lines. I want to replace characters 7 through 14 of every line with 0000000 Input: 12345678901234567890 23456789012345678901 Output 12345600000004567890 23456700000005678901 Please help. JaK (9 Replies)
Discussion started by: jakSun8
9 Replies

9. UNIX for Dummies Questions & Answers

Replace Special characters in a file

Hi, I have a data like this in a file, 402003279034002000100147626030003300010000000000002000029000000 ær^M^\MÍW^H I need to replace those special char to some other char like # or $ Is there any ways to do it... I tried commands tr,sed and many but it was not able to replace because... (1 Reply)
Discussion started by: solai
1 Replies

10. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies
Login or Register to Ask a Question