The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to search and replace text in same file Vrgurav Shell Programming and Scripting 1 04-25-2008 03:20 AM
Replace text in input file daphantomica Shell Programming and Scripting 2 04-25-2008 12:39 AM
automating file search and replace text ommatidia Shell Programming and Scripting 3 02-28-2008 01:40 PM
Replace line of text in a file colinchase UNIX for Dummies Questions & Answers 3 10-04-2001 06:54 AM
replace text in a file from the command line... dudboy UNIX for Dummies Questions & Answers 1 09-04-2001 11:31 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-30-2005
Registered User
 

Join Date: Aug 2003
Location: Ireland
Posts: 269
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?
Reply With Quote
Forum Sponsor
  #2  
Old 12-30-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Code:
sed -e 's_^\(0232 \)red\( testing4\)$_\1blue\2\nInfo:Line changed_g'
Reply With Quote
  #3  
Old 12-31-2005
Registered User
 

Join Date: Aug 2003
Location: Ireland
Posts: 269
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?
Reply With Quote
  #4  
Old 01-01-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
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
Reply With Quote
  #5  
Old 01-01-2006
mona's Avatar
Registered User
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0