Long lines in test.awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Long lines in test.awk
# 8  
Old 10-17-2010
I have a file test.awk whick I call using -f and need to know what I should write, the first or the second one??

Code:
print $1,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,
         $2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,
         $2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2

or like this

Code:
print $1,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,  \
         $2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,              \
         $2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2



---------- Post updated at 03:32 PM ---------- Previous update was at 03:29 PM ----------

I am using printf, changing the code from

Code:
print $1,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,c1,c2,
            c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,
            c2,c1,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2,$2

to

Code:
        printf "%-8.1f", $1, FS
        for (i=1;i<=13;i++) printf "%-10.5f", $2 FS
        printf "%-10.5f %-10.5f", c1, c2, FS
        for (i=1;i<=11;i++) printf "%-10.5f", c3, FS
        printf "%-10.5f %-10.5f", c1, c2, FS
        for (i=1;i<=13;i++) printf "%-10.5f", $2 FS
        printf RS

Changing FS is a good idea

---------- Post updated at 03:41 PM ---------- Previous update was at 03:32 PM ----------

The only problem I might have with OFS="\t" is that the tab might be too big.

Can one change the tab size in awk?

---------- Post updated at 04:03 PM ---------- Previous update was at 03:41 PM ----------

I have coded something like this but can't get the columns to align

Couple of lines like this

Code:
        printf "%-8.1f", $1, FS
        for (i=1;i<=15;i++) printf "%-10.5f", $2, FS
        for (i=1;i<=11;i++) printf "%-10.5f", $2, FS
        for (i=1;i<=15;i++) printf "%-10.5f", $2, FS
        printf RS

then some like this

Code:
        printf "%-8.1f", $1, FS
        for (i=1;i<=12;i++) printf "%-10.5f", $2 FS
        printf "%-10.5f %-10.5f %-10.5f", c1, c2, c3 FS
        for (i=1;i<=11;i++) printf "%-10.5f", ca FS
        printf "%-10.5f %-10.5f %-10.5f", c3, c2, c1 FS
        for (i=1;i<=12;i++) printf "%-10.5f", $2 FS
        printf RS

and more like the beginning

Code:
        printf "%-8.1f", $1, FS
        for (i=1;i<=15;i++) printf "%-10.5f", $2, FS
        for (i=1;i<=11;i++) printf "%-10.5f", $2, FS
        for (i=1;i<=15;i++) printf "%-10.5f", $2, FS
        printf RS

I am getting


Code:
8.5     1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   1.96947   
9.0     2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   2.05162   
9.5     2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.09723   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   2.12917   
10.0    2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.16864   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.13562   2.16864   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   2.20167   
10.5    2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.23562    2.20158   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.16753   2.23562    2.20158   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   2.26967   
11.0    2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.29874    2.26374    2.22873   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.19373   2.22873    2.26374    2.29874   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   2.33375   
11.5    2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.35859    2.32267    2.28676   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.25084   2.28676    2.32267    2.35859   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   2.39451   
12.0    2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.41610    2.37930    2.34251   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.30572   2.34251    2.37930    2.41610   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   2.45289   
12.5    2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.47223    2.43458    2.39694   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.35929   2.39694    2.43458    2.47223   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   2.50988   
13.0    2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.52734    2.48886    2.45037   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.41188   2.45037    2.48886    2.52734   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   2.56583   
13.5    2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.58203    2.54271    2.50339   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.46407   2.50339    2.54271    2.58203   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   2.62135   
14.0    2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.63677    2.59661    2.55646   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.51630   2.55646    2.59661    2.63677   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   2.67692   
14.5    2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.69163    2.65064   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.60965   2.69163    2.65064   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   2.73262   
15.0    2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.74652   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.70470   2.74652   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   2.78835   
15.5    2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.80117   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   2.84383   
16.0    2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   2.89912   
16.5    2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   2.95447   
17.0    3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   3.00996   
17.5    3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   3.06547   
18.0    3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.12072   3.

# 9  
Old 10-17-2010
I noticed that in section 1 and 3 you are using $2, FS and in section 2 that deviates you are using $2 FS without a comma, but in both case FS will not get printed. Also in section 2 in two separate lines you have spaces in the format but not after the third format defnition.
I would just put the space in the formatting everywhere, if you need it for example
Code:
for (i=1;i<=15;i++) printf "%-10.5f ", $2

or just leave them out everywhere.

Last edited by Scrutinizer; 10-17-2010 at 07:35 PM..
# 10  
Old 10-17-2010
Quote:
Originally Posted by rdcwayx
Code:
printf $1 FS; for (i=1;i<=50;i++) printf $2 FS; printf RS

Using possibly untrusted data as a format string is very poor practice. In C, that would be a massive security hole; in AWK, it's merely unwise.

Code:
printf "%s", $1 FS; for (i=1;i<=50;i++) printf "%s", $2 FS; printf "%s", RS

Regards,
Alister
# 11  
Old 10-17-2010
I'm trying to patch up something with what you guys have said.
# 12  
Old 10-18-2010
Here's an idea: why don't you put the repeated printing inside a function, like this:
Code:
function pr(x,r,i) 
{ 
   for(i=1;i<=r;i++) printf "%-10.5f",x+0
}

Then you can call it like this:
Code:
{
   printf "%-8.1f", $1+0
   pr($1,15)
   pr($1,11) 
   pr($1,15)
   print ""

   printf "%-8.1f", $1+0
   pr($1,12)
   pr(c1,1); pr(c2,1); pr(c3,1)
   pr($1,11) 
   pr(c3,1); pr(c2,1); pr(c2,1)
   pr($1,12)
   print ""
}

I used +0 to make sure the value gets interpreted as numeric.
# 13  
Old 10-18-2010
Very neat. Will use it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed and awk for long lines

Hi, I'm trying make a variable length file to a fixed length of 4000. I'm to pad spaces on the right of a record if length is less than 4000 to make the record length 4000. I'm trying to use the below commands awk '{printf "%-4000s\n", $0}' inputfile.dat > outputfile.dat sed -e :a... (12 Replies)
Discussion started by: uxusr
12 Replies

2. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

3. Shell Programming and Scripting

Long line displayed as multiple lines in Outlook. Please help!

When a file generated by a shell script is sent as a mail (not as an attachment), in MS Outlook, single lines of the file are displayed as two/three lines (as the lines are long). Is there a way to make each line to be displayed in 'single line with scroll bar' to enable viewing of the long line? (2 Replies)
Discussion started by: thulasidharan2k
2 Replies

4. Shell Programming and Scripting

Long line displayed as multiple lines in Outlook. Please help!

When a file generated by a shell script is sent as a mail (not an attachment), in MS Outlook, single lines of the file are displayed as two/three lines (as the lines are long). Is there a way to make each line to be displayed in 'single line with scroll bar' to enable viewing of the long line? ... (0 Replies)
Discussion started by: thulasidharan2k
0 Replies

5. Shell Programming and Scripting

Pull specific lines from long file based on formula

For people who want to pull a number of lines from a long file using a specific formula n (number of iterations in a loop) a (offset number) b (stretch factor) example with n {1..100} for (( n=1; n<101; n++ )); do awk -v n=$n 'NR==a+(b*n)' a=0 b=1 inputfile >>outputfile (2 Replies)
Discussion started by: sgruenwald
2 Replies

6. Shell Programming and Scripting

splitting long string into several lines?

I'm using a barcode scanner to grab ISBNs. Unfortunately, short of hitting "enter" each time (not easy while on a ladder), there's no good way to split it up. So I scanned it into a series of long lines in notepad. Now, I need to split each line into 12-number lines. instead of:... (4 Replies)
Discussion started by: mbourgon
4 Replies

7. Shell Programming and Scripting

Breaking long lines into (characters, newline, space) groups

Hello, I am currently trying to edit an ldif file. The ldif specification states that a newline followed by a space indicates the subsequent line is a continuation of the line. So, in order to search and replace properly and edit the file, I open the file in textwrangler, search for "\r " and... (14 Replies)
Discussion started by: rowie718
14 Replies

8. UNIX for Dummies Questions & Answers

Lines too long error

Hi Gurus, I have a big file having around 5000 lines. What I need to do is as below. $cat myfile aaaa bbbb ccc ddd ... I want the output to be as below: 'aaaa,'bbbb','cccc'.... For this I have written something like this code: vi sac.txt |nawk '{printf NR","}'>ss code: (2 Replies)
Discussion started by: thana
2 Replies

9. UNIX for Dummies Questions & Answers

removing lines in a too long file

HI, i need to remove about a 3000000 lines in a "too long file", without using vi editor. Im using hp-ux 11.0. (4 Replies)
Discussion started by: Goodfella
4 Replies

10. Shell Programming and Scripting

Re: Long command lines

Hello, AIM: Need to test for the presence of some files (*.F) in a certain directory. having a problem with this line is ksh: if test `ls $SOMEDIR/dir/*.F \ 2>/dev/null|wc -w` -eq 0 Basically testing for the presence of *.F files in the specified directory. If the return... (4 Replies)
Discussion started by: enoch
4 Replies
Login or Register to Ask a Question