printing something on the same line 
Author Message
 printing something on the same line

Hi *,

I want to print $1 and $2 on the same line.
Normally, on can do the print $1$2 , but i have a
condition in between:

print $1 : if ($2=3) {print "What the $#%? Dollar2="$2 }

And now i get the What the $#%? Dollar2="$2 on a new
line.
The normal \c  (as in echo "hello \c" )
doesn't work - what's the trick in nawk?

Tnx,
S



Mon, 17 Mar 2003 03:00:00 GMT  
 printing something on the same line

Quote:

> Hi *,

> I want to print $1 and $2 on the same line.
> Normally, on can do the print $1$2 , but i have a
> condition in between:

> print $1 : if ($2=3) {print "What the $#%? Dollar2="$2 }

> And now i get the What the $#%? Dollar2="$2 on a new
> line.
> The normal \c  (as in echo "hello \c" )
> doesn't work - what's the trick in nawk?

> Tnx,
> S

Use printf instead of print.

e.g.: printf("%s",$1);if(condition)printf("%s\n",$2)

Tristan.



Mon, 17 Mar 2003 03:00:00 GMT  
 printing something on the same line

Quote:

> I want to print $1 and $2 on the same line.
> Normally, on can do the print $1$2 , but i have a
> condition in between:

> print $1 : if ($2=3) {print "What the $#%? Dollar2="$2 }

> And now i get the What the $#%? Dollar2="$2 on a new line.

Are you sure you mean '$2=3', that is, assign value 3 to 2nd field?

Use  printf  for each output statement, which only prints a
newline \n when explicitely told to do so; or use ternary
operator ?:
  print x, (y==3 ? ("What the $#%? Dollar2=" y) : "")

Quote:
> The normal \c  (as in echo "hello \c" ) doesn't work

"normal"? no "\c" whatever that might be in *awk.

--
  All true believers shall break their eggs at the convenient end.



Mon, 17 Mar 2003 03:00:00 GMT  
 printing something on the same line

Quote:

> I want to print $1 and $2 on the same line.

    { print $1 $2 }

Quote:
> ... but i have a condition in between:
> print $1 : if ($2=3) {print "What the $#%? Dollar2="$2 }

    { print $1 ($2 == 3 ? "What the $#%? Dollar2=" $2 : $2) }

        or

    { print $1 ($2 == 3 ? "What the $#%? Dollar2=" : "") $2 }

Quote:
> ... what's the trick in nawk?

The conditional expression. Read about it on pp. 37-38 of _The
AWK Programming Language_ <http://cm.bell-labs.com/cm/cs/awkbook/>.

--
Jim Monty

Tempe, Arizona USA



Mon, 17 Mar 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Search file and print line + next line?

2. Printing line by line

3. The pretty-printer, *print-lines* and per-line prefixes

4. Print formatting - same line printing?

5. Match something then process next line

6. LOGO-L> Do something after something else

7. (something) /= something ?

8. awk won't print multiple lines

9. print out the continuation line

10. awk print variable on a new line

11. print contents up to blank line

12. printing next line ....

 

 
Powered by phpBB® Forum Software