Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk's equiv to vi's J

13 views
Skip to first unread message

nwh

unread,
Dec 8, 2009, 2:35:20 PM12/8/09
to
Hi,
Given the cleaned up output of...

for i in `cat hostnames.list`
do
nslookup ${i}
done > names.txt

I'm left with...

host1name.com
10.10.10.1
host2name.com
10.10.10.2
host3name.com
10.10.10.3

I've been using an uppercase J in vi to manually move the hostname's
IP address up onto the same line as it's hostname.

I'd love to automate this and thought awk's sub() function might be
the key but I can't even get it to sub X for Y, much less locate
"hostname.com$" and then send in a "J" (ascii 074). Any thoughts/
suggestions will be appreciated. Tks /n

Kenny McCormack

unread,
Dec 8, 2009, 2:43:24 PM12/8/09
to
In article <f6d95da0-99ca-415e...@r40g2000yqn.googlegroups.com>,

nwh <neilw...@gmail.com> wrote:
>Hi,
>Given the cleaned up output of...
>
>for i in `cat hostnames.list`
>do
>nslookup ${i}
>done > names.txt
>
>I'm left with...
>
>host1name.com
>10.10.10.1
>host2name.com
>10.10.10.2
>host3name.com
>10.10.10.3
>
>I've been using an uppercase J in vi to manually move the hostname's
>IP address up onto the same line as it's hostname.

ORS=NR%2?" ":"\n"

Yes, that's the whole program!

nwh

unread,
Dec 8, 2009, 3:07:40 PM12/8/09
to
On Dec 8, 2:43 pm, gaze...@shell.xmission.com (Kenny McCormack) wrote:
> In article <f6d95da0-99ca-415e-89e8-688de3bb6...@r40g2000yqn.googlegroups.com>,

>
>
>
>
>
> nwh  <neilwhar...@gmail.com> wrote:
> >Hi,
> >Given the cleaned up output of...
>
> >for i in `cat hostnames.list`
> >do
> >nslookup ${i}
> >done > names.txt
>
> >I'm left with...
>
> >host1name.com
> >10.10.10.1
> >host2name.com
> >10.10.10.2
> >host3name.com
> >10.10.10.3
>
> >I've been using an uppercase J in vi to manually move the hostname's
> >IP address up onto the same line as it's hostname.
>
> ORS=NR%2?" ":"\n"
>
> Yes, that's the whole program!

Hi Kenny. Thanks for the quick reply.
I've been playing with awk for a while now and was beginning to
question my sanity until I read a post by Ed Morton about awk in
Solaris. I'm a Solaris admin and didn't know awk was broken in
Solaris. I tried nawk on some stuff I thought should have worked but
didn't, and it worked! Make me crazy.

This is not to say that any of my stuff was as slick, short, or simple
as yours and for that, I thank you.

Rgds,
/n

Ed Morton

unread,
Dec 8, 2009, 3:57:53 PM12/8/09
to

or:

ORS=NR%2?FS:RS

Fore!

Ed.

Kenny McCormack

unread,
Dec 8, 2009, 5:02:43 PM12/8/09
to
In article <hfmekh$v2t$1...@news.eternal-september.org>,
Ed Morton <morto...@gmail.com> wrote:
...

>> ORS=NR%2?" ":"\n"
>
>or:
>
> ORS=NR%2?FS:RS

Cute. I like it.

>Fore!

Indeed!

Ed Morton

unread,
Dec 8, 2009, 10:55:45 PM12/8/09
to

I'm sure I didn't invent it, I probably just picked it up somewhere, but the
thing I like about it especially is how much awk it covers in such a short
example. I mean in those 14 characters we've introduced output record
separators, field separators, input record separators, the condition-action
paradigm, ternary operators, and default actions - that's almost a significant
concept every 2 characters! Explaining it would make a good interview exam
question for awk programmers ;-).

Ed.

Janis

unread,
Dec 8, 2009, 11:33:08 PM12/8/09
to
On Dec 9, 4:55 am, Ed Morton <mortons...@gmail.com> wrote:
> Kenny McCormack wrote:
> > In article <hfmekh$v2...@news.eternal-september.org>,

> > Ed Morton <mortons...@gmail.com> wrote:
> > ...
> >>> ORS=NR%2?" ":"\n"
> >> or:
>
> >> ORS=NR%2?FS:RS
>
> > Cute. I like it.
>
> I'm sure I didn't invent it, I probably just picked it up somewhere,

I've used that idiom at least in a posting back in 2007 (and mayve
even
earlier than that). Anyway, whenever that was; I haven't seen it
before
I used it. So, if no one complains I'd claim being the inventor. :-}

Janis

> [...]

Kenny McCormack

unread,
Dec 9, 2009, 12:00:26 AM12/9/09
to
In article <3b5f4530-1fa2-4d35...@c3g2000yqd.googlegroups.com>,

I'm 98% sure that I personally invented the basic idea (ORS=... as the
pattern, with no action - i.e., default action). Ed's enhancement was
using FS and RS instead of hardcoding space and newline. It's nice for
two reasons:
1) Saves a few golf strokes
2) Is more "portable" (or "logical", if you look at it that way) in
that if FS and RS had been assigned non-default values, they
would be used.

Also, as he says, it is a very instructive 14 characters of AWK code.

Janis

unread,
Dec 9, 2009, 12:16:03 AM12/9/09
to
On Dec 9, 6:00 am, gaze...@shell.xmission.com (Kenny McCormack) wrote:
> In article <3b5f4530-1fa2-4d35-97e2-222e43a60...@c3g2000yqd.googlegroups.com>,

>
>
>
> Janis <janis_papanag...@hotmail.com> wrote:
> >On Dec 9, 4:55 am, Ed Morton <mortons...@gmail.com> wrote:
> >> Kenny McCormack wrote:
> >> > In article <hfmekh$v2...@news.eternal-september.org>,
> >> > Ed Morton <mortons...@gmail.com> wrote:
> >> > ...
> >> >>> ORS=NR%2?" ":"\n"
> >> >> or:
>
> >> >> ORS=NR%2?FS:RS
>
> >> > Cute. I like it.
>
> >> I'm sure I didn't invent it, I probably just picked it up somewhere,
>
> >I've used that idiom at least in a posting back in 2007 (and mayve
> >even
> >earlier than that). Anyway, whenever that was; I haven't seen it
> >before
> >I used it. So, if no one complains I'd claim being the inventor. :-}
>
> >Janis
>
> >> [...]
>
> I'm 98% sure that I personally invented the basic idea (ORS=... as the
> pattern, with no action - i.e., default action).

Yes, I seem to recall so, as well; that you've invented using ORS
without explicite action.

My idiom (at that time) was using the builtin variables instead of
string constants.

Actually (in the mentioned posting) this expression

BEGIN{OFS="\t"}{ORS=NR%2?OFS:RS)}$1=$1

which, today, wouldn't make a birdy. ;-)

Janis

Ed Morton

unread,
Dec 9, 2009, 11:19:53 AM12/9/09
to

Well, someone had better patent this quick before Bill Gates sees it and makes
more millions...

Ed.

Janis

unread,
Dec 9, 2009, 4:56:07 PM12/9/09
to

:-)

My _hope_ is that material that was published in a public place (like
Usenet) becomes hard to patent. But legislation seems to vary a lot
across national borders, and, frankly, I am unsure whether that's
effectively true in any country at all, nowadays.

Janis

>
> Ed.

0 new messages