Not specific to CentOS but I know you guys would be really helpful anyhow. Basically, I have a file which has been editted in the past very similarly to the hosts file only now I want to use it as a hosts file and need to run some fancy sed to massage the data into shape. Currently, the data in the file is in the form of <ip address> <tab> <short hostname> <space> <short hostname alias>. In some cases there may not be any aliases so the end of line would be right after the short hostname (no space at the end either). In other cases there could be many space separated short hostname aliases. What I have been trying to do without success is add our domain name to the first string after the ip address and tab character. As an example,
== Before ==
1.1.1.1 foo 10.10.10.10 bar bar2 100.100.100.100 foobar foobar2 foobar3
== After ==
1.1.1.1 foo.contoso.com 10.10.10.10 bar.contoso.com bar2 100.100.100.100 foobar.contoso.com foobar2 foobar3
Any advice on how to pull this off? Thanks.
On Fri, May 23, 2008 at 11:41 AM, Scott McClanahan SMcClanahan@forterrainc.com wrote:
Not specific to CentOS but I know you guys would be really helpful anyhow. Basically, I have a file which has been editted in the past very similarly to the hosts file only now I want to use it as a hosts file and need to run some fancy sed to massage the data into shape. Currently, the data in the file is in the form of <ip address> <tab> <short hostname> <space> <short hostname alias>. In some cases there may not be any aliases so the end of line would be right after the short hostname (no space at the end either). In other cases there could be many space separated short hostname aliases. What I have been trying to do without success is add our domain name to the first string after the ip address and tab character. As an example,
== Before ==
1.1.1.1 foo 10.10.10.10 bar bar2 100.100.100.100 foobar foobar2 foobar3
== After ==
1.1.1.1 foo.contoso.com 10.10.10.10 bar.contoso.com bar2 100.100.100.100 foobar.contoso.com foobar2 foobar3
Any advice on how to pull this off? Thanks.
I'd use awk. Put the lines in a file, then do this
cat test.txt | awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }'
On Fri, May 23, 2008 at 8:50 AM, Matt Shields mattboston@gmail.com wrote:
I'd use awk. Put the lines in a file, then do this
cat test.txt | awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }'
Or just awk '{ print $1 "\t" $2 ".centos.com\t" $3 "\t" $4 }' test.txt
newhostsfile
(The cat just complicates things, as with most cats.... :-)
mhr
On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote:
Not specific to CentOS but I know you guys would be really helpful anyhow. Basically, I have a file which has been editted in the past very similarly to the hosts file only now I want to use it as a hosts file and need to run some fancy sed to massage the data into shape. Currently, the data in the file is in the form of <ip address> <tab> <short hostname> <space> <short hostname alias>. In some cases there may not be any aliases so the end of line would be right after the short hostname (no space at the end either). In other cases there could be many space separated short hostname aliases. What I have been trying to do without success is add our domain name to the first string after the ip address and tab character. As an example,
== Before ==
1.1.1.1 foo 10.10.10.10 bar bar2 100.100.100.100 foobar foobar2 foobar3
== After ==
1.1.1.1 foo.contoso.com 10.10.10.10 bar.contoso.com bar2 100.100.100.100 foobar.contoso.com foobar2 foobar3
Any advice on how to pull this off? Thanks.
sed 's/ /.contoso.com '
Cheers,
Mihai
On Fri, May 23, 2008 at 06:02:29PM +0200, Mihai T. Lazarescu wrote:
On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote:
1.1.1.1 foo 10.10.10.10 bar bar2 100.100.100.100 foobar foobar2 foobar3
== After ==
1.1.1.1 foo.contoso.com 10.10.10.10 bar.contoso.com bar2 100.100.100.100 foobar.contoso.com foobar2 foobar3
Any advice on how to pull this off? Thanks.
sed 's/ /.contoso.com '
That works because we've explicity been told <ip address> <tab> <short hostname> <space> <short hostname alias> so the first space is a good place to switch.
More generally, if there's possibly multiple spaces or tabs in multiple places this might work:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
Stephen Harris wrote:
On Fri, May 23, 2008 at 06:02:29PM +0200, Mihai T. Lazarescu wrote:
On Fri, May 23, 2008 at 08:41:19AM -0700, Scott McClanahan wrote:
1.1.1.1 foo 10.10.10.10 bar bar2 100.100.100.100 foobar foobar2 foobar3
== After ==
1.1.1.1 foo.contoso.com 10.10.10.10 bar.contoso.com bar2 100.100.100.100 foobar.contoso.com foobar2 foobar3
Any advice on how to pull this off? Thanks.
sed 's/ /.contoso.com '
That works because we've explicity been told <ip address> <tab> <short hostname> <space> <short hostname alias> so the first space is a good place to switch.
More generally, if there's possibly multiple spaces or tabs in multiple places this might work:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
The above version easier to read and "copy paste". Space is space and tabe is \t
sed 's/^([^ \t]*[ \t]*[^ \t]*)([ \t]*.*)$/\1.contoso.com\2/'
On Fri, May 23, 2008 at 06:59:24PM +0200, Thomas Johansson wrote:
Stephen Harris wrote:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
The above version easier to read and "copy paste". Space is space and tabe is \t
sed 's/^([^ \t]*[ \t]*[^ \t]*)([ \t]*.*)$/\1.contoso.com\2/'
I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example).
Stephen Harris wrote:
On Fri, May 23, 2008 at 06:59:24PM +0200, Thomas Johansson wrote:
Stephen Harris wrote:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
The above version easier to read and "copy paste". Space is space and tabe is \t
sed 's/^([^ \t]*[ \t]*[^ \t]*)([ \t]*.*)$/\1.contoso.com\2/'
I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example).
perl can do anything sed can do and has almost no platform or version related syntax differences - plus it has \s to represent 'whitespace' and you don't have to bang your head on the wall when you are half done and realize you have to do something spanning multiple lines.
on 5-23-2008 11:51 AM Les Mikesell spake the following:
Stephen Harris wrote:
On Fri, May 23, 2008 at 06:59:24PM +0200, Thomas Johansson wrote:
Stephen Harris wrote:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
The above version easier to read and "copy paste". Space is space and tabe is \t
sed 's/^([^ \t]*[ \t]*[^ \t]*)([ \t]*.*)$/\1.contoso.com\2/'
I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example).
perl can do anything sed can do and has almost no platform or version related syntax differences - plus it has \s to represent 'whitespace' and you don't have to bang your head on the wall when you are half done and realize you have to do something spanning multiple lines.
Show your example. Inquiring minds want to know!
Scott Silva wrote:
sed 's/^([^ ]*[ ]*[^ ]*)([ ]*.*)$/\1.contoso.com\2/'
(where there's a space *and* a TAB inside each of the [ ] )
The above version easier to read and "copy paste". Space is space and tabe is \t
sed 's/^([^ \t]*[ \t]*[^ \t]*)([ \t]*.*)$/\1.contoso.com\2/'
I grew up with versions of 'sed' that don't understand this new-fangled method of specifying tabs, and write enough cross-platform code that I can't rely on it (still doesn't work in Solaris 10, for example).
perl can do anything sed can do and has almost no platform or version related syntax differences - plus it has \s to represent 'whitespace' and you don't have to bang your head on the wall when you are half done and realize you have to do something spanning multiple lines.
Show your example. Inquiring minds want to know!
perl -p -e 's/(\s+\S+)/$1.contoso.com /'
That's "match one or more whitespace characters followed by one or more non-whitespace and add .contoso.com after whatever matched.