[SOLVED] Sed - Replacing only text with several specific lines excluded
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sed - Replacing only text with several specific lines excluded
As much as I didn't want to ask a sed question, especially considering there's already one on this page I've looked as best I could and cant find the solution.
Id like to use sed to replace occurrences of a pattern but exclude two or 3 specific lines that are not consecutive.
For example I know with 1,10 i could exclude the first 10 lines, what is the syntax if I just wanted to exclude line 3 and 7.
The sed command I'm working with right now is for rearranging Ethernets.
cat /etc/udev/rules.d/70-persistent-net.rules | sed -e '/'"$found1fullmac"'/!s/eth1/'"found1eth"'/' > /etc/udev/rules.d/70-persistent-net.rules
I would like to replace $found1fullmac with two variables representing line numbers to exclude from the replacement.
Thanks for any help, sorry if its a simple question but I coulden't find it in any of the reference information.
You can use variable for 3, 7 and your exchange string the same way you have in your sed.
I swapped out some of the earlier commands using cat/sed for your abbreviated version and they work great.
However I cannot get the awk command to run, I must have some sort of mistake in the syntax but i cant get it to work. It ends up wiping the contents of the entire file (from copying a black file using > I assume)
with no success. I tried mixing around the quotes abit and still no luck. Ill be using variables for the line numbers but i figured this would simplify things for now.
I have even tried with no variables in hopes of just finding the problem
this will delete lines 3 and 7, replace otherwise:
Code:
sed -e "3d;7d; s/eth0/<your replacement>/ " file
If you want to use variables:
Code:
sed -e "$line1del d; $line2del d; s/eth0/<your replacement>/ " file
It gets really tricky if you do not know in advance how many lines you want to replace. In this case you could define one variable as follows:
Code:
scriptvar="3d;7d;11d;"
and then use for sed:
Code:
sed -e "$scriptvar s/eth0/<your replacement>/ " file
However, from your post I assume that your variable, that contains the lines to delete might look like:
Code:
found1fullmac="3 7"
echo $found1fullmac
3 7
So the lines are space seperated. In this case you can do:
Code:
sed -e "$(echo $found1fullmac|sed -e 's/ /d;/;s/$/d;/') s/eth0/<your replacement>/" file
Let us know if found1fullmac is "formatted" otherwise.
Hi,
Unfortunately what I'm looking to do isn't to delete the two lines 3 and 7 but simple not replace the text on them. For the final file I need the same as the input file except the pattern has been replaced on all the lines except 3 and 7.
As for number of lines it will always be just two lines, but thats for that info I will bookmark it in case I need it later.
Unfortunately what I'm looking to do isn't to delete the two lines 3 and 7 but simple not replace the text on them. For the final file I need the same as the input file except the pattern has been replaced on all the lines except 3 and 7.
As for number of lines it will always be just two lines, but thats for that info I will bookmark it in case I need it later.
Sorry, I misunderstood. Well, in that case
Code:
sed -e "$line1 ! {$line2 ! {s/eth1/<replacement>/}}" file
will replace 'eth1' with '<replacement>' except for the lines specified in the variables 'line1' and 'line2'.
[EDIT]
Use sed's -i option to make the changes permanent.
Well I can't beat the sed (damn . . knew there was a brace in there somewhere), but the best way to learn the awk would be to apply it without the variable(s) first.
Although I did miss one quite important piece ... check out the 1 at the end of awk statement:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.