search and replace a specific text in text file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search and replace a specific text in text file?
# 1  
Old 06-24-2008
overwriting a line in text file

I have a text file with following content (3 lines)

filename : output.txt

first line:12/12/2008
second line:12/12/2008
third line:Y


I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before.

I tried using cat output.txt |sed -e 's/third line:Y/third line:Y/' , but in this case it writes the output to some other file and we have to replace that old file with new file.

Do we have a way to do this without replacing the old file with new file after using sed command or can we do this any command other than sed?

Last edited by santosham; 06-24-2008 at 12:16 PM..
# 2  
Old 06-24-2008
Code:
sed -i 's/third line:Y/third line:N/' your-file

# 3  
Old 06-24-2008
its says .. 'i' is an illegal option

thanks
# 4  
Old 06-25-2008
your sed does not support the inline edit -i option.

using Perl:
Code:
perl -pi -e 's/third line:Y/third line:N/' output.txt

# 5  
Old 06-25-2008
Regarding only changing the third line:

$ cat f
yy
yy
yy
yy
yy
$ sed '3s/yy/nn/' f
yy
yy
nn
yy
yy

Does this help?

Last edited by eBay; 06-25-2008 at 07:35 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. Shell Programming and Scripting

Search field in text file and replace value

Hi there, First of all this is my first post here. Thank you in advance for your help. What I am trying to do is the following. I have a text file where each field of each row is separated by a tabulator. Looks like this: ATOM 1 N HSE A 26 3.033 -10.429 -2.262 1.00 17.07 ... (8 Replies)
Discussion started by: doom4
8 Replies

3. Shell Programming and Scripting

find from file and replace with specific text

Dear All, I do not have any knowledge of scripting. I want to replace specific lines of a text file with a specific text. Like I have one file which is "original file" and one file "changes file" which has list of lines which I want to replace in original file with a specific string. I want the... (5 Replies)
Discussion started by: libras
5 Replies

4. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Emergency UNIX and Linux Support

Search and replace in text file

Hi, I have gigabytes of text files that I need to search for "&" and replace with "&amp". Is there a way to do this efficiently (like sed command)? Hope you could help. Thanks. (17 Replies)
Discussion started by: daytripper1021
17 Replies

6. Shell Programming and Scripting

text file search and replace with awk

hello all greeting for the day i have a text file as the following text.xml abcd<FIELD>123.456</FIELD>efgh i need to replace the value between <FIELD> and </FIELD> by using awk command. please throw some light on this. thank you very very much Erik (5 Replies)
Discussion started by: erikshek
5 Replies

7. Shell Programming and Scripting

search a file for specific text

Hello everyone, I'm a newbie here. I'm working on a ksh script on Solaris 5.10 and have a question. I'm trying to search for a word line by line in a log file, and grab the text right after it inside quotes, and assign that to a variable. For example, each line in the log will look something... (6 Replies)
Discussion started by: Kristin_in_CO
6 Replies

8. Shell Programming and Scripting

search and replace a text in a file

Hi all, I have a requirement where i have to search data between strings 'SELECT' and ';' and replace this text as "SELECT.....;" so that i can export this extracted string into a excel cell. Please suggest on this. (5 Replies)
Discussion started by: goutam_igate
5 Replies

9. Shell Programming and Scripting

How to search and replace text in same file

script is as below v_process_run=5 typeset -i p_cnt=0 pdata=/home/proc_data.log while do # execute script in background dummy_test.sh "a1" "a2" & p_cnt=$p_cnt+1 echo "data : $p_cnt : Y" >> $pdata done file created with following data in... (1 Reply)
Discussion started by: Vrgurav
1 Replies

10. Shell Programming and Scripting

automating file search and replace text

Hi, I am trying something like this: Let's say I have a file called File1 with contents: x=-0.3 y=2.1 z=9.0 I have another file, File2, with contents: xx= yy= zz= (nothing after "="). What I want to do is get the value of x in File1 and set it to xx in File2, i.e., xx=-0.3. And the... (3 Replies)
Discussion started by: ommatidia
3 Replies
Login or Register to Ask a Question