Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing lines and adding new line - sed

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
HI,

In josts file I have :

# 128.100.0.1 token0sample # token ring name/address
# 10.2.0.2 x25sample # x.25 name/address
127.0.0.1 loopback localhost # loopback (lo0) name/address

10.4.30.161 levwtiger
10.4.31.253 timna5 lev

I want use sed to :

1.Remove all lines after 127.0.0.1 one.
2.Add new line with hostname+IP.

Thanks


Long live king Moshiach !
 
Hi

Code:
awk '1;$1=="127.0.0.1"{exit}END{print "1.2.3.4\t host.name\t# comment"}' /input/file > /output/file

[gray]# or[/gray]

sed '/^127.0.0.1/{a1.2.3.4\thost.name\t# comment    
q}' /input/file > /output/file
Note, that the [tt]sed[/tt] solution works with GNU [tt]sed[/tt], is possible to fail on Unix.

Feherke.
 
Hi,

I did both expressions - removing lines after 127.0.0.1 and adding a line with a host:

sed -e "/^[0-9]/s/.*//g" -e '/^127.0.0.1/{i1.2.3.4\tlev1234\tq}' /etc/hosts

Get error:
sed: Function /^127.0.0.1/{i1.2.3.4\tlev1234\tq} cannot be parsed.

Also - the above removal expression removes the 127.0 ... line as well - how do I actually remove all lines after the one with 127.0 ... ?

Long live king Moshiach !
 
Hi

You must keep those [tt]sed[/tt] commands in separate lines. The [tt]a[/tt] ( append ) command must be finised with a line break. By the way, if you use the [tt]i[/tt] ( insert ) command, the new line will appear before the matched line.

Try this one too, I hope is more portable :
Code:
[ignore]sed '/^127.0.0.1/{p;s/.*/1.2.3.4\thost.name\t# comment/;q}' /input/file > /output/file[/ignore]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top