Beginning awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginning awk question
# 1  
Old 10-22-2013
Beginning awk question

I have a report that I'd like to print columns headers on. The code appears as follows:

Code:
gawk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'|gawk '{ printf "%-78s%-30s%-30s%-30s\n", $1,$2, $3, $4 }' |awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' report_1020

I am trying to make it print a report with column headers and am really shaky with the BEGIN statement:

Code:
[gawk 'BEGIN {print "NAME RATE HOURS FINE\n"}' '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'|gawk '{ printf "%-78s%-30s%-30s%-30s\n", $1,$2, $3, $4 }' |awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }' report_1020

When I attempt to print this report what I get are nice columns but no report! I have tried to search on different sites but many give horizontal row headers and I am unable to find what to do next.

Awk gurus? If you could help.. greatly appreciated!

What I want to do is this:

Name Rate Hours Fine

Tracey 10 13 1
Burt 9 12 2
# 2  
Old 10-22-2013
Good to see someone trying to learn awk, a powerful and useful language, though a bit strange until you learn the assumptions behind it.

All of the code needs to be inside the same ' ' block. By putting the next statement into a separate ' ' block you are telling awk that it's a filename.

Whenever you find yourself doing awk | awk | awk, you should probably stop and think how it can be done in one awk. (Usually but not always.)

What does your input look like? You've posted your output.. I think..

Last edited by Corona688; 10-22-2013 at 01:34 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-22-2013
Your BEGIN statement is fine, but, as Corona688 says, continue the script in the same '...' block.

But, what are you doing there? In the first script, you are printing absolute values of fields, even if you know they're text strings. Not an error, but pointless. In the second script, all you do is formatting the output to some extreme wide, space padded columns. Not an error, but pointless if you pipe that to the next awk, which uses any number of spaces as field separator, so your formatting is gone. Plus, you again calculate absolute values, knowing that all numbers are positive from your first awk.

Try this (and consult the awk man page for the conditional assignments):
Code:
awk 'BEGIN {print "NAME RATE HOURS FINE\n"} { printf "%-78s%-30s%-30s%-30s\n", $1, ($2>=0)?$2:-$2, ($3>=0)?$3:-$3, ($4<0)?-$4:$4 }' report_1020

This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-22-2013
You are both right!

I see now what is wrong. It does need to continue as Corona said. I kept on typing and typing not noticing that the ' was what was stopping it from acting as it should and continuing to pick up the rest of the script. This was really helpful. I agree also my code is very messy and therefore everyone knows it is from a beginner; I will look at Rudi's examples to clean it up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Retrieve Beginning of file name and sort question

I am trying to look through a directory where the file names are like this: stringnumber_string, and I want to see if I can take the first part of the name and sort it by the number. I tried this: for i in *_; do if; then fileNum=${i%_*}; echo $fileNum fi done but it gives me an error.... (13 Replies)
Discussion started by: totoro125
13 Replies

2. Shell Programming and Scripting

One of the awk column spilling on the new line from beginning

I am having a problem where the Compute field/column is spilling over to the new line. Here's the piece of code and the o/p:- Code Sniplet:- for id in `qstat -u "*" -q "$Queue" -s r |sed -n '3,$ p'|awk -F" " '{print $1}'` do NODELIST=`cat /scratch/$id.hostlist.*|awk -F" " '{print $1,$2}'|tr '... (5 Replies)
Discussion started by: grish
5 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Removing from beginning to first : using awk

A have a file npt02-sr40-syn-dc0p014-32x24drw.log:0. Best Value = 0.00144914 npt02-sr40-syn-dc0p014-32x24drw.log:1. Best Value = 0.00115706 npt02-sr40-syn-dc0p014-32x24drw.log:2. Best Value = 0.00094345 npt02-sr40-syn-dc0p014-32x24drw.log:3. Best Value = 0.000925552... (11 Replies)
Discussion started by: kristinu
11 Replies

5. Programming

Beginning C++

I want to learn C++ ! :) I have basic programming knowledge with python and perl. Does anyone know of any great beginner tutorials for C++? (1 Reply)
Discussion started by: cbreiny
1 Replies

6. Shell Programming and Scripting

Swapping lines beginning with certain words using sed/awk

I have a large file which reads like this: fixed-address 192.168.6.6 { hardware ethernet 00:22:64:5b:db:b1; host X; } fixed-address 192.168.6.7 { hardware ethernet 00:22:64:5b:db:b3; host Y; } fixed-address 192.168.6.8 { hardware ethernet 00:22:64:5b:db:b4; host A; }... (4 Replies)
Discussion started by: ksk
4 Replies

7. Shell Programming and Scripting

awk question

Hi I have a multi -line file which is sorted by the 1-st colomn in the following format: 400 0000 0001 1000 1010 0111 0000 1000 0000 402 1101 0000 1100 1010 0111 1000 1000 0000 403 1001 0000 1100 1010 0111 0000 1000 0000 495 1000 0000 1100 1010 0111 0000 1000 0000 500 0000 0001 1000 0010... (2 Replies)
Discussion started by: aoussenko
2 Replies

8. Shell Programming and Scripting

Using NR with two variables at the beginning of awk

hi My requirement is this: I have a file having around 100000 records pipe delimited. Now I want to compare record 1 with record 2 and similarly record3 with record 4, this goes on.. For this purpose i put a script as follows: #!bin/ksh ct_line=1 nxt_line=`expr ${ct_line} + 1` awk -F "|"... (1 Reply)
Discussion started by: ramkrix
1 Replies

9. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

10. UNIX for Dummies Questions & Answers

the very beginning

Actually, I had my first problem before even BEGINNING using my FreeBSD. The installation guide said I should run the rawrite tool and should do something like A> rawrite if i put the the file I want to image-copy and the rawrite.exe in the same directory somewhere. OK done that. Next it asks... (6 Replies)
Discussion started by: Mudrack
6 Replies
Login or Register to Ask a Question