For Args and Nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For Args and Nawk
# 1  
Old 12-19-2011
For Args and Nawk

I am trying to write a simple shell script that will take certain arguments (numerical values) and plug each one into a nawk command.

I thought I would need to use for args x y z but i get syntax errors:
Code:
for args 16 1 3 25 31 41
do
nawk -F, '{if($10==$ && $12~/CA[1-5]$/){SUM+=$13}}END{print(SUM)}' /opt/PlexDC/statsdir/PegCount/secondary/*20111217*
done

So the script is supposed to take each argument (16, 1, 3, 25, etc) and plug into the nawk command where it says "$10==$", and $ is supposed to be the argument.

Smilie

Heather

Last edited by radoulov; 12-19-2011 at 04:15 PM.. Reason: Code tags!
# 2  
Old 12-19-2011
You're close. Your for syntax is slightly off, and getting it "into" awk isn't always obvious.

Code:
for arg in 16 1 3 25 31 41
 do
 nawk -F, -v arg=$arg '{if($10==arg && $12~/CA[1-5]$/){SUM+=$13}}END{print(SUM)}' /opt/PlexDC/statsdir/PegCount/secondary/*20111217*
 done

EDIT: Forgot to mention that the -v arg=$arg assigns the shell variable's value to the awk variable allowing it to be referenced in the awk programme. I also changed 'args' to 'arg' as it holds only one value at a time and the plural (IMHO) is misleading.

Last edited by agama; 12-19-2011 at 01:46 PM.. Reason: clarification
# 3  
Old 12-19-2011
You haven't even fed 'args' into nawk anywhere. Usually you'd do so by nawk -v VARNAME="$shellvar"

so
Code:
for arg in 16 1 3 25 31 41
do
        nawk -F, -v ARG="$arg" '{if($10==ARG && $12~/CA[1-5]$/){SUM+=$13}}END{print(SUM)}' /opt/PlexDC/statsdir/PegCount/secondary/*20111217*
done

This will run awk 6 separate times, with a different value of ARG every time, and print all of their output directly into the terminal. Is this precisely what you want?
# 4  
Old 12-19-2011
Thank you!! Happy Holidays!

---------- Post updated at 01:25 PM ---------- Previous update was at 12:14 PM ----------

Ok, so another question...this command works getting yesterday's date calculated:

Code:
date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'

But I want to get that calculation into the original command I had where the "*2011217*" is located (so I want my script to automatically run these summaries on yesterday's file, and not have to change the script each day for the new date).
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-19-2011 at 04:15 PM.. Reason: code tags, please!
# 5  
Old 12-19-2011
Quote:
Originally Posted by he204035
Thank you!! Happy Holidays!

---------- Post updated at 01:25 PM ---------- Previous update was at 12:14 PM ----------

Ok, so another question...this command works getting yesterday's date calculated:

date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'


But I want to get that calculation into the original command I had where the "*2011217*" is located (so I want my script to automatically run these summaries on yesterday's file, and not have to change the script each day for the new date).
No, it doesn't. What happens on the first of March?
Search the FAQs on how to do the date math.
# 6  
Old 12-19-2011
I figured it out!

This works!

Code:
date +%Y%m%d | awk '{printf"%8d\n",($1-1)}'

Thanks for your help.

Last edited by zxmaus; 12-19-2011 at 09:41 PM..
# 7  
Old 12-19-2011
I'll reiterate what vgersh99 asked before: what happens when the date command returns something like 20120101? 20120100 is an invalid date, so beware that your code doesn't handle edge conditions. In short you must do the math individually for year, month and day taking into account the number of days in each month, and leap year.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem parsing args

Heya Tooltip: Parsing (getopts) for -u successfully sets mode=umnt, but case umnt is not executed, instead it either executes/show help or regular mount screen. I had copy pasted the structure of a getopts 'structure' from Man Page for getopts (posix Section 1) - The UNIX and Linux Forums... (1 Reply)
Discussion started by: sea
1 Replies

2. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

3. Shell Programming and Scripting

Store args passed in array but not the first 2 args

Store args passed in array but not the first 2 args. # bash declare -a arr=("$@") s=$(IFS=, eval 'echo "${arr}"') echo "$s" output: sh array.sh 1 2 3 4 5 6 1,2,3,4,5,6 Desired output: sh array.sh 1 2 3 4 5 6 3,4,5,6 (2 Replies)
Discussion started by: iaav
2 Replies

4. UNIX for Dummies Questions & Answers

Parameters/Args

Hello, i have a problem. I must write a script, which wants 2 arguments. for example: ./test.sh x.txt y.txtit must be write x.txt in y.txt and when i give 1 or no argument like /.test.sh x.txtmust this give a error message like: SYNTAX <inputfile> <outputfile> my solution is... (5 Replies)
Discussion started by: eightball
5 Replies

5. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

6. UNIX for Dummies Questions & Answers

args of 50+ files

Hey.. I've gotten inspired by another thread and used this: #!/usr/bin/bash args 2,5 $(<file.list) It works but I'll like the results separated into different files or back into the 'used'/original files, whatever is easiest. e.g. use fileA | args 2,5 > fileB or fileA and then do this to... (9 Replies)
Discussion started by: lost
9 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Programming

Command line args

My program usage takes the form for example; $ theApp 2 "one or more words" i.e. 3 command line arguments; application name, an integer, some text My code includes the following 4 lines: int anInteger; char words; sscanf(argv, "%d", &anInteger); sscanf(argv, "%s", &message); Based... (2 Replies)
Discussion started by: enuenu
2 Replies

9. UNIX for Dummies Questions & Answers

alias with args how to ...

Hello ( sorry newbie question ) I don't understand something im trying to make simple alias that takes 1 arg but it don't gives me the desire result here is what I have : stlist | awk '{print "ls -l "$2}' now I want to translate it to alias that takes instead of the $2 one arg so I did : ... (4 Replies)
Discussion started by: umen
4 Replies

10. Shell Programming and Scripting

Args to Array

Hello all, I have a question. Please help me to populate an array with the arguments passing to a Shell scripts. For example when I call "abc.sh a1 a2 a3" args (a1, a2, ...) recieved in an Array inside the abc.sh arr = a1 arr = a2 and so on... Thanks in advance, (2 Replies)
Discussion started by: Shaz
2 Replies
Login or Register to Ask a Question