Replace 8th and 9th characters in file permanently


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace 8th and 9th characters in file permanently
# 1  
Old 11-02-2011
Replace 8th and 9th characters in file permanently

Good evening,

I have a file and wish to replace the 8th and 9th characters on the first line only no matter what they are with 44 and the file permanantly changed.

e.g. file example.txt before change:

123456789123456
hi how are you
blah
blah


file example.txt after change:

123456744123456
hi how are you
blah
blah


All answers much appreciated.
# 2  
Old 11-02-2011
Try:
Code:
perl -i -pe 's/(.{7})../${1}44/ if $.==1' example.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-03-2011
Works perfectly.

Many thanks.

---------- Post updated 11-03-11 at 04:11 AM ---------- Previous update was 11-02-11 at 06:36 PM ----------

Morning,


I have now been asked for it in a bash script.

No perl.



Any ideas

Thanks in advance.
# 4  
Old 11-03-2011
Code:
 
$ sed  '1 s/\(.......\)..\(.*\)/\144\2/' test 
123456744123456
hi how are you
blah
blah

---------- Post updated at 03:10 PM ---------- Previous update was at 03:09 PM ----------

Code:
 
$ sed '1 s/\(.\{7\}\)../\144/' test
123456744123456
hi how are you
blah
blah

# 5  
Old 11-03-2011
If your file is big, the following will take no time:
Code:
echo 44 |dd of=test bs=1 seek=7 count=2 conv=notrunc

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ommit the numbers or any characters only at 8th columns after the dot (.).

Ommit the numbers or any characters only at 7th or 8th columns after the dot (.) . Since the group column has 1 and 2 spaces. Thanks -rw-r--r--. 1 user1 domain users 619 2017-04-13 16:16:50.284598383 +0000 aa drwxr-xr-x. 2 root root 6 2017-05-08... (8 Replies)
Discussion started by: invinzin21
8 Replies

2. Shell Programming and Scripting

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. abcdefghijklmn 123456789011 excepted result: abcxxxghijklmn 123xxx789011 Thanks in advance. (6 Replies)
Discussion started by: ken6503
6 Replies

3. Solaris

9th field in shadow file

Hi experts, Can somebody explain, what is 9th field in /etc/shadow ? The last digit - (5 Replies)
Discussion started by: solaris_1977
5 Replies

4. AIX

removing 8th and 9th character of a FILENAME

Hi everyone, I have a file called pdf.txt which has the following list inside: 7110412_1_31122012.pdf 7286510_4_46667100.pdf 4002176_1_00018824.pdf ... I need a looping script to REMOVE the 8th and 9th character of the filenames listed, no matter what character that is. So... (4 Replies)
Discussion started by: chipahoys
4 Replies

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

6. Shell Programming and Scripting

Find and replace permanently

i have a few scripts in which i need to find string"ali1@abcd.com" and replace it with "ali@abcd.com" i used 2 below commands but none of them is permanently replacing the old string in the script s.sh perl -pi -e 's/ali1@abcd.com/ali@abcd.com/g' s.sh sed 's/ali1@abcd.com/ali@abcd.com/g'... (7 Replies)
Discussion started by: ali560045
7 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

How would I replace the 9th character of every line in a file?

Is there a simple way to replace the 9th character of every line in a text file? Right now it is a space and instead I want to stick in a 0, every line is the same. Is there a quick way to do this in Unix? (Solaris) (5 Replies)
Discussion started by: LordJezo
5 Replies
Login or Register to Ask a Question