Replace specific letter in a file by other letter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific letter in a file by other letter
# 1  
Old 05-13-2015
Replace specific letter in a file by other letter

Good afternoon all,
I want to ask how to change some letter in my file with other letter in spesific line

eg.
data.txt
Code:
1
1
1
0
0
0
0

for example i want to change the 4th line with character 1.
How could I do it by SED or AWK.

I have tried to run this code but actually did not modify the data.txt file and just print the result to screen.
Code:
sed -e "4s/0/1/" data.txt

# 2  
Old 05-13-2015
That's how it works. Redirect the output to a temp file and then, mv the temp file to data.txt, overwriting it.
# 3  
Old 05-13-2015
Or, use ed instead of sed:
Code:
ed data.txt <<-EOF
	4s/0/1/
	w
	q
EOF

or:
Code:
printf '%s\n' '4s/0/1' w q | ed data.txt

# 4  
Old 05-15-2015
With GNU sed and BSD sed, there is the -i option for in-place editing, but there the recommend option is to have it automatically create a backup file as well..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace space in column with letter for several rows

I have a pbd file, which has the following format: TITLE Protein X MODEL 1 ATOM 1 N PRO 24 45.220 71.410 43.810 1.00 0.00 ATOM 2 H1 PRO 24 45.800 71.310 42.000 1.00 0.00 TER ENDMDL Column 22 is the chain... (5 Replies)
Discussion started by: Egy
5 Replies

2. Shell Programming and Scripting

Replace the first letter of each line by a capital

Hi, I need to replace, as the title says, the first letter of each line (when it's not a number) by the same letter, but capital. For instance : hello Who 123pass Would become : Hello Who 123pass Is there a way with sed to do that ? Or other unix command ? Thank you :) (7 Replies)
Discussion started by: ganon551
7 Replies

3. Shell Programming and Scripting

Multiplying lines of file that contain certain letter by value

I am trying to remove the last letter in a file and then multiply each line (which contained this letter) by 500. This is what I have: 1499998A 1222222A 1325804A 1254556 1235 9998 777 cat /tmp/listzz |gawk '{print $4}'|gawk '{gsub(//, ""); print } This removes the A... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. Shell Programming and Scripting

Program to match the id and replace one letter in the content

Hi all, I have one file with a sequence and the other file which says the position and the letter to be changed. I have to match two files and replace content. Example is shown which will describe what I want to do. For example, file 1 has many sequences and few are shown below sequence file:... (6 Replies)
Discussion started by: kaav06
6 Replies

5. Homework & Coursework Questions

C++ homework, finding a letter from a file

Write a program which asks user to enter a string and that string saves in a .txt file. After the file has been saved your program must count how many time letter 'a' has been reapeated ? Use fstream, string, and cctype libraries to make your jobe easier. I wrote following code #include... (1 Reply)
Discussion started by: solaris_user
1 Replies

6. Shell Programming and Scripting

Get all File names starting with letter P

Hi, I have lets say 10 files , I need to process them one by one. So I need a command to get one file name at a time to process it into a variable Example Files P1111.dat P3344.dat S344.dat ... v_file_name = 'p111.dat' .. I will rename it to something after processing ... (1 Reply)
Discussion started by: prassu
1 Replies

7. UNIX for Dummies Questions & Answers

find positions of a letter in a text file

Hi, I would like to know how can I get all the positions of a letter, let say letter C in a text file. sample input file: hcck pgog hlhhc desired output file: 2 3 13 Many thanks! (2 Replies)
Discussion started by: fadista
2 Replies

8. UNIX for Dummies Questions & Answers

Display File with a specific letter

Hi im a noob in Unix Do you guys know what command display you the files that have the character n in there name im not looking that they have the n in the beginning or in the end im looking that in search the entire string to see if it have the character n i try this ls n* but only show... (2 Replies)
Discussion started by: Kaziduz
2 Replies

9. Solaris

Is it possible to setup a samba share to always mount to a specific Windows drive letter???

I'm trying to setup Samba in a solaris zone... Is there a way to setup Samba so that every Windows machine that tries to connect to the share always gets it mounted under the same drive letter (e.g. H:)??? My Samba share (in smb.conf) /home/pickup I want that all Window users get it mounted... (3 Replies)
Discussion started by: verdepollo
3 Replies

10. UNIX for Dummies Questions & Answers

i need 100th occurance of a letter in file

Hi to all, I am looking a file in vi editor to get 100th occurance of a latter in that file. Can any one help me in this? Thanks Sathish (1 Reply)
Discussion started by: bsathishmca
1 Replies
Login or Register to Ask a Question