awk - print formatted without knowing no of cols


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - print formatted without knowing no of cols
# 1  
Old 08-29-2006
awk - print formatted without knowing no of cols

Hi,
i want to print(f) the content of a file, but i don't know how many columns it has (i.e. it changes from each time my script is run). The number of columns is constant throughout the file.

Any suggestions?
# 2  
Old 08-29-2006
check man awk

Try to use NF of awk..
# 3  
Old 08-29-2006
Thanks. I managed to write the following script:
Code:
#!/bin/bash

awk '{
for ( i=1; i<=NF; i++)
print $i }
' $1

Unfortunately all output is written in one column/field. I want it with the correct number of fields. This may sound awkward, and maybe it is, i could of course just do a 'print', but then i will not get a pretty listing when my values are of different lengths.

So, what i have is for example:
Code:
1021 203213 1.1E-03
114432 0.77 2.7e-08
1234 2020 5.0e-05

and i want a tab separated output f. ex:
Code:
1021    203213  1.1E-03
114432  0.77    2.7e-08
1234    2020    5.0e-05

# 4  
Old 08-29-2006
Code:
#!/bin/bash

awk '{
  for ( i=1; i<=NF; i++)
     printf("%s%s", $i, (i==NF) ? "\n" : "\t")
}' $1

# 5  
Old 08-29-2006
Great! Thanks!
# 6  
Old 09-04-2006
This is just what I was looking for but ...
I used this and got an errors. I run it in ksh - does it matter ?

awk '{ for ( i=6; i<=NF; i++) printf("%s%s", $i, ($i==NF) "\n":"\t") }' ./ERRORS.txt
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks
# 7  
Old 09-04-2006
Sorry this was exactly what I run

$awk '{ for ( i=1; i<=NF; i++) printf("%s%s", $i,(i==NF) ? "\n" : "\t")}' ./ERRORS.txt
awk: syntax error near line 1
awk: illegal statement near line 1
$
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing a formatted file with awk

Hi - I want to interrogate information about my poker hands, sessions are all recorded in a text file in a particular format. Each hand starts with the string <PokerStars> followed by a unique hand reference and other data like date/time. There is then all the information about each hand. My first... (5 Replies)
Discussion started by: rbeech23
5 Replies

2. Shell Programming and Scripting

awk operation to get table formatted file

I need to parse .conf file in Unix system to get output from the snippets like below : ; generated by mod_identity exten => int,1,GoSub(mdc_template-3,s,1) exten => int,n(next_380),Return() ; default action for busy exten => ext,1,GoSub(mdc_template-3,s,1) exten => ext,n,Set(PRI_CAUSE=17)... (2 Replies)
Discussion started by: cheecky
2 Replies

3. Shell Programming and Scripting

As knowing which version has the awk?

friends How do I know which version of awk using my linux? (2 Replies)
Discussion started by: tricampeon81
2 Replies

4. Shell Programming and Scripting

Problems with awk printf, formatted output

Hi, i have a script, which is incomplete, am on my way developing it. Input 1,12,2012,IF_TB001 2,12,2012,3K3 3,Z56,00000,25,229,K900,00, ,3G3, ,USD, ,0000000000,000, , , , 550000000 3,Z56,00000,53,411,W225,00,000, , ,USD,OM170,0000000000,000, , , , -550000000 4,Z56,COUNT, 4,SUM LOC,... (19 Replies)
Discussion started by: selvankj
19 Replies

5. Shell Programming and Scripting

awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file but I don't know the number of fields for example, in the following line Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///,... (3 Replies)
Discussion started by: esolvepolito
3 Replies

6. Shell Programming and Scripting

awk -- print combinations for 2 cols

Dear all, could you please help me with awk please? I have such input: Input: a d b e c f The number of lines is unknown before reading the file. I need to print possible combination between the two columns like this: Output: a d b d c d a e b e c e a f (2 Replies)
Discussion started by: irrevocabile
2 Replies

7. Shell Programming and Scripting

print formatted text to the printer

Hello!!! I am using shell script that print some formated text on the screen (example) ======== hello I am ... ======== Is it possible to print this information to the printer exactly as I see it on the screen??? (6 Replies)
Discussion started by: tdev457
6 Replies

8. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

9. UNIX for Dummies Questions & Answers

Problem with formatted printing in AWK

Hi, I want to print 130 fileds using formatted printing in AWK. It looks like awk '{printf ("%7.2f%7.2f...%7.2\n",$1,$2,...,$130)}' inflie>oufile But it gives me an error: Word too long! Can you please help me with this? Is there another way to do this? (1 Reply)
Discussion started by: PHL
1 Replies

10. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies
Login or Register to Ask a Question