![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#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? |
| Forum Sponsor | ||
|
|
|
#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? |
|
#4
|
||||
|
||||
|
Quote:
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:
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 |
||||
| Google The UNIX and Linux Forums |