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
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..
How did you manage to get that "Info:Line changed" on the same line.
See what I got:
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
Anyway, from I understand, this sed should work
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
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
/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