Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Output

Status
Not open for further replies.

desanti

Programmer
Jul 9, 2004
51
US
I have a variable number of input fields.i wish to output them and have as the last field a line number.i wish the line numbers to be lined up vertically.so that if i have 3 fields or seven fields or whatever,the last field would have the line number(or any other identifier),line up as
if it were a column.thanks to all.
 
Take a look at printf function, eg:
printf "%-80.80s %03d\n",$0,NR

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV:thanx for the input.i tried the code and it did as you said,when output to the terminal.when output to a file the numbers did not line up.?? what am i missing??
 
Can you please post the whole exact code you use ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
nawk '{printf("%-80.80s %3d\n",$0,NR)}' tmp.txt > xtmp.txt
if i do NOT output to xtmp.txt the numbers lineup fine.
this is done via a command line(no -f).thanx
 
for 'cron' escape your '%' with '\'

man crontab:
The sixth field of a line in a crontab file is a string
that is executed by the shell at the specified times. A per-
cent character in this field (unless escaped by \) is
translated to a NEWLINE character.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
PHV: i forgot to address the answer to your response on my formatting problem.i have tried to find out how to respond
correctly but got no answer and also how to insert code into the message.any help??
 
How do you know the alignment is not the same when output is to a file ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV: I tried it just as the code was written.thank you.
 

If you want the linenumber always to start in the same column:
Code:
nawk '{printf("%-75s %d\n",$0,NR)}'
 
futurelet: thanx for the input,however the result is the same as i informed PHV.if i print to the screen,fine,if i print to a file not fine.it is evidently dependent upon the line length;see below.
aasdfasdfasdf 001
asdfasdfasdfasdf 002
hope this helps??
 
Could tabs be causing the problem?
[tt]
nawk '{gsub(/\t/," ");printf("%-75s %d\n",$0,NR)}'
[/tt]
 
futurelet:thanx again but no go.i looked at the input file
using a hex editor--no tabs.what is confusing is that it works fine to the screen.i am in a windows enviornment using a ksh shell.most of what i try works fine,either command line or -f.i am out of ideas!!i do appreciate your effort.i will keep trying and let you know.i imagine you have better things to do!!if a divine intervention occurs or you get an inspiration let me know.thanx again.
 
futurelet: somehow,some responses that were not for me were getting to me and i answered without looking.did you get the prior response that if the input file was >= to 51 characters your original idea worked! sorry,about that.mea culpa.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top