awk (or C) question
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > awk (or C) question




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    awk (or C) question  
Ben


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

How do I repeat the format specification for a printf line, i.e. I'd
rather write:

printf {"5%s","a","b","c","d","e"}

than:

printf {"%s%s%s%s%s","a","b","c","d","e"}

Hopefully you see what I'm trying to do in the first statement, but
obviously it gives me 5a5b5c5d5e since this isn't the proper shortcut.
Thanks.

Ben





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Ben


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Ben wrote:
> How do I repeat the format specification for a printf line, i.e. I'd
> rather write:
>
> than:
>
> printf {"%s%s%s%s%s","a","b","c","d","e"}
>

I suppose I should clarify that the problem would be better stated as
wanting to do (note the \n):

printf {"5%s\n",a,b,c,d,e}

but still have a, b, etc. on the same line. Based on my previous post an
acceptable response would have been simply

printf {"%s",a,b,c,d,e}

Thanks again.





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Ben wrote:
> Ben wrote:
> 
>
> I suppose I should clarify that the problem would be better stated as
> wanting to do (note the \n):
>
> printf {"5%s\n",a,b,c,d,e}
>
> but still have a, b, etc. on the same line. Based on my previous post an
> acceptable response would have been simply
>
> printf {"%s",a,b,c,d,e}
>
> Thanks again.

In the subject you write "awk (or C) question", but I don't recognize
this as an awk or C syntax.

Using ksh93 both of these statements

printf {"%s\n",a,b,c,d,e}

printf "%s\n" a b c d e

(using the shell built-in printf) produce the output

a
b
c
d
e


Janis





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Ben


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Janis Papanagnou wrote:
> I don't recognize
> this as an awk or C syntax.
>

echo "dummy" | awk '{printf ( "%s\n","hi")}'





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

On 2006-03-15, Ben wrote:
> How do I repeat the format specification for a printf line, i.e. I'd
> rather write:
>
> printf {"5%s","a","b","c","d","e"}
>
> than:
>
> printf {"%s%s%s%s%s","a","b","c","d","e"}
>
> Hopefully you see what I'm trying to do in the first statement, but
> obviously it gives me 5a5b5c5d5e since this isn't the proper shortcut.

It wouldn't give you that in awk or C; it would in the shell,
whether you use a shell built-in or the external command.

In the shell, you probably want, (but you didn't specify exactly
what you want):

printf "%s" "a" "b" "c" "d" "e"

In awk (or C):

{ printf( "%s%s%s%s%s","a","b","c","d","e" ); }


--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |          is released under the
2005, Apress                 |     GNU General Public Licence





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Ben


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Chris F.A. Johnson wrote:
>
>     It wouldn't give you that in awk or C; it would in the shell,
>     whether you use a shell built-in or the external command.

Your right. I'm not truly in awk, using awk in a shell.

>
>     In the shell, you probably want, (but you didn't specify exactly
>     what you want):

Yeah, I noticed this and replied to my own message on a different branch
of this thread. What I really want is something (in a shell) like

printf "%s\n" a b c

but get output of

abc

instead of

a
b
c

Sorry for the poorly posed question.





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Ben


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Ben wrote:
> Janis Papanagnou wrote:
> 
>
> echo "dummy" | awk '{printf ( "%s\n","hi")}'

you're right. This isn't awk syntax, it's shell use of awk. I got
confused for a minute but Chris Johnson (other branch of this thread)
straightened me out.





[ Post a follow-up to this message ]



    Re: awk (or C) question  
laura fairhead


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

On Wed, 15 Mar 2006 14:24:10 -0500, Ben <none@none.com> wrote:

>Ben wrote: 
>
>I suppose I should clarify that the problem would be better stated as
>wanting to do (note the \n):
>
>printf {"5%s\n",a,b,c,d,e}
>
>but still have a, b, etc. on the same line. Based on my previous post an
>acceptable response would have been simply
>
>printf {"%s",a,b,c,d,e}

Hi Ben,

What's the problem with simply typing it out ??

If it is just a question of generating this source code (and nothing to do
with run time) the natural solution is to use a macro. Unfortunately 'awk'
doesn;t have them and as far as I know C macros are not powerful enough.

If you were generating these printfs() from data (the only application
I can think of for the function you requested) then the generator program
itself would write this code and would therefore know how many %s's
and what not to produce for the format arguments... but that would
show how even without MACROs in the target language you would do this
in general - ie; put unexpanded source into a file (with its own
extension ) then run your macro expanding program on it to produce
the proper C/awk statements. This would work for either C or awk
and there are already professional tools which you can use for this
without having to write your own, mmmm .... maybe something like
'm4' I think is what you use for this ( "man m4" )

Otherwise what you can do (will make runtime operation a little less
efficient) is to write a repeatstring function and use that in the
format argument, eg in 'awk';

function repstr(n,s){
for(q="";n--;) q=q""s
return q
}

BEGIN{
printf(repstr(5,"%s\n"),1,2,3,4,5)
}

bestwishesfrom
laura



>
>Thanks again.

--
echo moc.12klat@daehriaf_arual|sed 's/\(.\)\(.\),*/\2,\1/g;h;s/,//g;/@t/q;G;
D'





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

On 2006-03-15, Ben wrote:
> Chris F.A. Johnson wrote: 
>
> Your right. I'm not truly in awk, using awk in a shell.
> 
>
> Yeah, I noticed this and replied to my own message on a different branch
> of this thread. What I really want is something (in a shell) like
>
> printf "%s\n" a b c
>
> but get output of
>
> abc
>
> instead of
>
> a
> b
> c

printf "%b" a b c d e '\n'

--
Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
Shell Scripting Recipes:     |  My code in this post, if any,
A Problem-Solution Approach  |          is released under the
2005, Apress                 |     GNU General Public Licence





[ Post a follow-up to this message ]



    Re: awk (or C) question  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-06 10:55 PM

Ben wrote:
> Ben wrote:
> 
>
>
> you're right. This isn't awk syntax, it's shell use of awk.

No, *this* (the above) *is* awk syntax. You had posted a different thing.

> I got
> confused for a minute but Chris Johnson (other branch of this thread)
> straightened me out.

You got your solutions, I guess...





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:01 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register