Visit The New, Modern Unix Linux Community


script - replace text in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script - replace text in file
# 1  
script - replace text in file

I have a file something like this (except much larger amount of rows/data)

0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 red testing4
2143 blue testing5
3453 blue testing6

In a script I want to replace the word red with blue where the line begins with a certain number - ie. in this case I want to replace the word red with blue where the line begins with 0232.
I also want to place a new line in this file after the one in which I replace red with blue... I want to add a line to state I replace the word red with the word blue... so final file would look like this:

0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4
info: I replaced red with blue in the above line
2143 blue testing5
3453 blue testing6


any ideas?
# 2  
Code:
sed -e 's_^\(0232 \)red\( testing4\)$_\1blue\2\nInfo:Line changed_g'

# 3  
ok - not quite what I am after



This is the result I get with your command based on the file I created test1
sed -e 's_^\(0232 \)red\( testing4\)$_\1blue\2\nInfo:Line changed_g' test1
0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4nInfo:Line changed
2143 blue testing5
3453 blue testing6

1st point - I will only know the number at the start of the line and the colour... I will not know what is contained on the rest of the line so I can use 'testing4' in my sed string

2nd point - this only returns the output to screen but the original file remains unchanged. I need to change the original file - bare in mind the original file could contain 300000 lines and I only want to change one..


Can you still help? Smilie
# 4  
Quote:
Originally Posted by frustrated1
sed -e 's_^\(0232 \)red\( testing4\)$_\1blue\2\nInfo:Line changed_g' test1
0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4nInfo:Line changed
2143 blue testing5
3453 blue testing6
How did you manage to get that "Info:Line changed" on the same line.

See what I got:

Code:
sh-2.05b$ echo "0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 red testing4
2143 blue testing5
3453 blue testing6" | sed -e 's_^\(0232 \)red\( testing4\)$_\1blue\2\nInfo:Line changed_g'
0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4
Info:Line changed
2143 blue testing5
3453 blue testing6

Quote:
Originally Posted by frustrated1
1st point - I will only know the number at the start of the line and the colour... I will not know what is contained on the rest of the line so I can use 'testing4' in my sed string

2nd point - this only returns the output to screen but the original file remains unchanged. I need to change the original file - bare in mind the original file could contain 300000 lines and I only want to change one..
Anyway, from I understand, this sed should work

Code:
sh-2.05b$ echo "0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 red testing4
2143 blue testing5
3453 blue testing6" | sed -e 's_^\(0232 \)red\(.*\)_\1blue\2\nInfo:Line changed_g'
0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4
Info:Line changed
2143 blue testing5
3453 blue testing6

# 5  
Try the below SED command,

Code:
sed 's_^\(0232 \)red\(.*\)$_\1blue\2\#you have to presss the enter button here
info: I replaced red with blue in the above line_' datafile.dat


Code:
/export/home/test/mons/UnixForum>echo "0001 blue testing1
> 0002 blue testing2
> 0006 blue testing3
> 0232 red testing4
> 2143 blue testing5
> 3453 blue testing6" | sed 's_^\(0232 \)red\(.*\)$_\1blue\2\
> info: I replaced red with blue in the above line_'
0001 blue testing1
0002 blue testing2
0006 blue testing3
0232 blue testing4
info: I replaced red with blue in the above line
2143 blue testing5
3453 blue testing6


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #620
Difficulty: Medium
Python implicitly assumes that each physical line corresponds to a logical line.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace text in a file

I wrote a program using Perl to find and replace a text within a file. The text that needs to be replaced in the file is 'sql7.0.1' with 'sqls715'. When I execute my program I get an error message: Here is my code: #!/usr/bin/perl use strict; use warnings; my $filename =... (6 Replies)
Discussion started by: dellanicholson
6 Replies

2. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

5. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

6. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

7. Shell Programming and Scripting

Shell Script to replace text

I need a little help with a shell script. I want to be able to specify parameters so that the script searches multiple files (specified as parameters) and only modifies the file if it finds the string of text. For the files it doesn't find the string of text within, it should leave it alone. ... (4 Replies)
Discussion started by: joebaber
4 Replies

8. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

10. UNIX for Dummies Questions & Answers

search and replace a specific text 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... (4 Replies)
Discussion started by: santosham
4 Replies

Featured Tech Videos