Find text and replace another field
Hi all,
I am wanting to be able to search and replace through linux files. Now I know how to do the simple search and replace, I want to be able to search, then move along that line a bit and replace another part. Here's an example that will help: File text.tmp: jase1, 'log1' 2000 jase2, 'log2' 3000 jase3, 'log3' 4000 I want to be able to search for jase1, then replace log1 with test1. After that I want to search again for jase1 and replace 2000 with 5000. Is this possible and if so, how do you do it? Any help is greatly appreciated. best regards, Jase |
make a scrip as below:
Quote:
|
I am after a script where I can find jase1 and replace whatever is between the ' '
Then to replace whatever is after the second ' |
Edit1: Sorry... didn't answer your question...
Edit2: Came up with a bash script that will solve your problem (hopefully): Code:
#!/bin/bash |
Here's one that is a bit more advanced ;) :
Code:
#!/bin/bash If you need others to use the script I suggest you replace the "no_args" function though... |
Thanks alot for that, will give them a try tomorrow and let you know if I have any problems.
|
Unless I am mistaken, this should work:
Code:
sed '/jase1/s/log1/test1/;/jase1/s/2000/5000/g' text.tmp > new_text.tmp The 's' is: Quote:
Hope that's what you were after! |
Quote:
Nice one! Definitly going in my "Excellent_Linux_Commands.txt"! I've made my own iptables generator, where my main problem was how to change .conf variables with the least hassle... You just simplified everything A LOT! Thanks m8! But the problem remains... What if you don't know the value of X (i.e. "jase1, 'X' XXXX")? Then you need to search for everything between '.*' and ' .*$... How can you search (with sed) for ' ? |
You could also just pipe one sed command into another:
Code:
sed '/jase1/s/log1/test1/g' < text.tmp | \ |
Excellent link!
And according to the text there, you can search for hex-ascii codes: sed '/jase1/s/\x27.*\x27/\x27<newlog-entry>\x27/g' <-- "\x27"="'", so now you can search and replace anything between ' and ' ('s/\x27.*\x27/\x27REPLACED\x27/g') Now there's no reason to use my LARGE script for something that can be done like this: Code:
localhost # sed -i '/jase1/s/\x27.*\x27/\x27test1\x27/;/jase1/s/\x27 .*/\x27 5000/g' text.tmp Cool! |
All times are GMT -5. The time now is 12:14 AM. |