LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-17-2010, 11:24 AM   #1
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Rep: Reputation: 1
Question 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.
 
Old 06-17-2010, 11:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Well I am not a sed guru, but agree it is probably possible. I thought I would point something out and show an awk alternative.

The thing to point out was the superfluous cat and multiple calls to the same file. Assuming what you have worked, this would be the equivalent:
Code:
sed -i -e '/'"$found1fullmac"'/!s/eth1/'"found1eth"'/' /etc/udev/rules.d/70-persistent-net.rules
And the awk alternative would be:
Code:
awk 'NR != 3 && NR != 7{sub(/eth1/,"<your change here>")}' /etc/udev/rules.d/70-persistent-net.rules > /etc/udev/rules.d/70-persistent-net.rules
You can use variable for 3, 7 and your exchange string the same way you have in your sed.
 
Old 06-17-2010, 12:25 PM   #3
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
Well I am not a sed guru, but agree it is probably possible. I thought I would point something out and show an awk alternative.

The thing to point out was the superfluous cat and multiple calls to the same file. Assuming what you have worked, this would be the equivalent:
Code:
sed -i -e '/'"$found1fullmac"'/!s/eth1/'"found1eth"'/' /etc/udev/rules.d/70-persistent-net.rules
And the awk alternative would be:
Code:
awk 'NR != 3 && NR != 7{sub(/eth1/,"<your change here>")}' /etc/udev/rules.d/70-persistent-net.rules > /etc/udev/rules.d/70-persistent-net.rules
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)

I have tried

Code:
awk 'NR != 8 && NR != 14{sub(/eth1/,"$found2eth")}' /etc/udev/rules.d/70-persistent-net.rules > /etc/udev/rules.d/70-persistent-net.rules
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

Code:
awk 'NR != 8 && NR != 14{sub(/eth1/,eth0)}' /etc/udev/rules.d/70-persistent-net.rules > /etc/udev/rules.d/70-persistent-net.rules
but no luck.

Where am I going wrong?

Thanks!
 
Old 06-17-2010, 02:38 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

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.

Last edited by crts; 06-17-2010 at 02:40 PM.
 
Old 06-17-2010, 03:15 PM   #5
Potatos
LQ Newbie
 
Registered: Apr 2010
Distribution: Fedora 13
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by crts View Post
Hi,

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.
 
Old 06-17-2010, 03:24 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Quote:
Originally Posted by Potatos View Post
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.
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.

Last edited by crts; 06-17-2010 at 03:26 PM.
 
Old 06-18-2010, 12:51 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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:
Code:
awk 'NR != 3 && NR != 7{sub(/eth1/,"<your change here>")}1' /etc/udev/rules.d/70-persistent-net.rules > /etc/udev/rules.d/70-persistent-net.rules
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 10:59 AM
[SOLVED] SED and Replacing Specific Line bridrod Linux - Newbie 6 08-24-2009 01:28 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 10:00 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 03:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration