Using Sed to remove part of line with regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Sed to remove part of line with regex
# 1  
Old 10-08-2010
Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire line.
Here's the example contents of the file:
Code:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
172.30.0.133  loghost

Here's the regex I was planning to use:
Code:
 /\bl.*(n|n6)\b/g

I tested it and it looks like its working. Now I just have to figure out how to use it in sed. The file needs to be left in place, so I know it'll be starting something along these lines...
sed -i (delete) /\bl.*(n|n6)\b/g /etc/hosts

Any help would be very much appreciated; I'm still getting used to using sed and 90% of the examples still look completely cryptic to me.

My ultimate goal is to have the /etc/hosts file look like this:
Code:
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost
::1             localhost6
172.30.0.133  loghost

# 2  
Old 10-08-2010
Are you sure you want to use that regex? It will match any hostname starting with "l" and ending with "n", so if your hosts file contains hostnames like "lion" etc, it will get deleted too. Try this instead:
Code:
 perl -i -pe 's/localhost6?\.localdomain6?//' /etc/hosts

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-08-2010
Awesome! That worked like gangbusters. I wasn't super worried about other hostnames since all hosts come out of the automation system with the same hostname, but this works just as well. Thanks!
# 4  
Old 10-08-2010
you can try this Smilie
Code:
# sed 's/localhost6*.localdomain6*//'

This User Gave Thanks to ygemici For This Post:
# 5  
Old 10-08-2010
Oh cool, I didn't know you could substitute with nothing Smilie Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove part of the line from start of the line?

Hello, I am java command from a shell script which will generate the below output on the command prompt signature Base64 :... (10 Replies)
Discussion started by: chetanojha
10 Replies

2. Shell Programming and Scripting

Get multi-line sed not to run if regex is found

Hello, everyone. Thanks for taking the time to read my post. I have nagios config files for which I'm adding the custom variable _mac_address. I have a sed script that places this variable into an existing file. The problem I'm having is if a line in the file is commented out, I don't want the... (2 Replies)
Discussion started by: JimBass
2 Replies

3. Shell Programming and Scripting

Perl regex to remove a segment in a line

Hello, ksh on Sun5.8 here. I have a pipe-delimited, variable length record file with sub-segments identified with a tilda that we receive from a source outside of our control. The records are huge, and Perl seems to be the only shell that can handle the huge lines. I am new to Perl, and am... (8 Replies)
Discussion started by: gary_w
8 Replies

4. Shell Programming and Scripting

Need sed help: find regex and if the next next line is blank, delete both

I've got a report I need to make easier to read Using sh on HP-UX 11.12. In short, I want to search for a regular expression and when found, examine the next line to see if it's blank. If so, then delete both lines. If not blank, move on to the next regexp. Repeat. So far I've got: ... (7 Replies)
Discussion started by: Scottie1954
7 Replies

5. UNIX for Dummies Questions & Answers

Remove part of file name with sed & mv

Ok, so I have bunch of files that are named "orange__file_name.asm" and I want to batch rename them to "file_name.asm" I know that using "ls | sed s/orange__//" will get rid of the part of the file name I do not want. But how do I combine that with the mv command to actually do it? Thanks JG (5 Replies)
Discussion started by: john galt
5 Replies

6. Shell Programming and Scripting

Selecting a part of the text (regex pattern, awk, sed)

Hello, let's start by giving you guys a few examples of the text: "READ /TEXT123/ABC123" "READ /TEXT123/ABC123/" "READ TEXT123/ABC123" "READ TEXT123/ABC123/" "READ TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123/" TEXT and ABC can be and I... (5 Replies)
Discussion started by: TehOne
5 Replies

7. Shell Programming and Scripting

remove range part of a file with sed

Hello, I would like to remove a range from a file with sed or any script command that is appropriate The section start by and finish by and I would like to keep line Could you tell me which command I should type ? Thanks a lot, Franck My input file is like this... (1 Reply)
Discussion started by: mfranck
1 Replies

8. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

9. Shell Programming and Scripting

Getting sed to work on part of a line

I been trying to get this right. I have trying to get rid of spaces in between the character < and the character >. Everytime I try, sed gets too greedy and do the whole line. Ex. < T AG 1> Hello, how are you doing? <Tag 2> I am doing fine. I want this: <TAG1> Hello, how are you... (6 Replies)
Discussion started by: quixoticking11
6 Replies

10. Shell Programming and Scripting

remove part of a line

Hi I need some help with using shell script to analyze the content of a file. I hope someone can help me. I have a file with content like the following: /foldera/database/procedure/a.proc$$/version1/2 /folderb/database/procedure/proj1/b.proc$$/version2/2 I need to write a shell script to... (16 Replies)
Discussion started by: tiger99
16 Replies
Login or Register to Ask a Question