|
Home > Archive > Unix Shell > March 2006 > awk (or C) question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
awk (or C) question
|
|
|
| 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
| |
|
| 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.
| |
| Janis Papanagnou 2006-03-15, 5: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
| |
|
| Janis Papanagnou wrote:
> I don't recognize
> this as an awk or C syntax.
>
echo "dummy" | awk '{printf ( "%s\n","hi")}'
| |
| Chris F.A. Johnson 2006-03-15, 5: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
| |
|
| 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.
| |
|
| 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.
| |
| laura fairhead 2006-03-15, 5: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'
| |
| Chris F.A. Johnson 2006-03-15, 5: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
| |
| Janis Papanagnou 2006-03-15, 5: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...
|
|
|
|
|