Awk question. 
Author Message
 Awk question.

It has been a while since I worked with awk and I have a question which
I hope someone can help me answer.  I have a hosts file which is broken
down in the following format. I do an awk '{print $1, $2, $3}' hosts.
This prints out the IP address hostname and alias.  There are hosts
(not every host) that have at least 15 alias's. I need awk to print out
all the fields on a line, but when it gets to a "#"(that is where the
remarks for the hosts start) I need it go on to the next line.  I have
tried pattern matching, but I didn't get the output I expected.  If
anyone can shed some light on this.

Thank you for you time and cooperation.
Henry W.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 11 May 2003 03:00:00 GMT  
 Awk question.
Quote:

> It has been a while since I worked with awk and I have a question which
> I hope someone can help me answer.  I have a hosts file which is broken
> down in the following format. I do an awk '{print $1, $2, $3}' hosts.
> This prints out the IP address hostname and alias.  There are hosts
> (not every host) that have at least 15 alias's. I need awk to print out
> all the fields on a line, but when it gets to a "#"(that is where the
> remarks for the hosts start) I need it go on to the next line.  I have
> tried pattern matching, but I didn't get the output I expected.  If
> anyone can shed some light on this.

I would remove the comments with sub and then print the rest of the
line:
awk '{sub(/#.*/,""); print}'
if you insist on awk. The same thing can be trivially done with sed.

Peter



Sun, 11 May 2003 03:00:00 GMT  
 Awk question.

[...]

% (not every host) that have at least 15 alias's. I need awk to print out
% all the fields on a line, but when it gets to a "#"(that is where the
% remarks for the hosts start) I need it go on to the next line.  I have

You could use # as the field separator, then print just $1:
 awk -F\# '{ print $1}' /etc/hosts

You could print fields until you find one that starts with #:
 awk '{ for (i = 1; i <= NF; i++) {
           if ($i ~ /^#/) break;
           printf "%s%s", i == 1 ? "" : OFS, $i
        }
        printf "\n"
 }' /etc/hosts
--

Patrick TJ McPhee
East York  Canada



Mon, 12 May 2003 10:40:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Simple awk question

2. A Newbie AWK question

3. awk question

4. awk question about matching a pattern

5. AWK question

6. newbie awk question

7. awk questions !!!

8. Simple awk question

9. Easy Awk Question

10. awk Question

11. awk question

12. awk question

 

 
Powered by phpBB® Forum Software