Sponsored Content
Full Discussion: Loop in Nawk Script
Top Forums UNIX for Dummies Questions & Answers Loop in Nawk Script Post 302469900 by Morph797 on Monday 8th of November 2010 03:03:23 PM
Old 11-08-2010
Code:
     1  # Parse input from the command line to look up a persons
     2  # employee record.
     3  BEGIN {
     4         system("clear")
     5         blank = " "
     6  if (ARGC > 2)
     7        {
     8         printf("argc = %d >%s >%s >%s",ARGC,ARGV[0],ARGV[1],ARGV[2])
     9         nvar = ARGV[2]
    10         delete ARGV[2]
    11        }
    12  else
    13        {
    14         printf("Please enter your name or emp# : > ")
    15         getline nvar < "/dev/tty"
    16       # getline var < "-"
    17         printf(">>>>> %s",nvar)
    18        }
    19         printf("\nName\t\t\t\tEmployee No.\tWeekly Pay\n\n")
    20        }
    21  # This section retrieves the total name from the DB
    22  # $0 ~ /\<nvar\>/ {
    23  $0 ~ nvar {
    24   numnames = NF-4
    25   name = $1
    26   for (var=2; var<=numnames; var++)
    27             {
    28              name = name blank $(var)
    29             }
    30  # This section calculates weekly pay
    31    wpay = $(NF) * $(NF-1)
    32  # This prints the output line
    33    printf("%-30s\t%s\t$ %.2f\n",name,$(NF-3),wpay)
    34   }
    35  END {
    36       printf(" \n")
    37      }

Moderator's Comments:
Mod Comment Please use [CODE][/CODE] tags to format listings et al.

Last edited by pludi; 11-08-2010 at 07:04 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing a for loop variable into nawk

After searching through books and the internet for days I can't seem to find an example of this. I'm trying to pass a variable from a for loop into nawk but can't seem to get all the syntax right. my script (thanks to anbu23 for nawk help) is this: for customers in `cat customers.txt` do... (3 Replies)
Discussion started by: Ant1815
3 Replies

2. Shell Programming and Scripting

help need on nawk script

Dear experts I have a big file containing several profiles each flagged with "PROFILE" at the beginning of each one. I am trying to use the following command in cshell to seperate each profile and save each one in seperate file. I wrote a script as following: nawk -v i=0 '{if($1~/PROFILE/)... (5 Replies)
Discussion started by: Reza Nazarian
5 Replies

3. Shell Programming and Scripting

NAWK Script not working fine

Hello to all can any one help me out with a nawk script. Actually i am having a shell script which uses nawk pattern searching and it is not parsing the file properly. I have been debugging it since long time, but nt able 2 find the root cause.. If any one can help me out with this one .. (3 Replies)
Discussion started by: dheeraj19584
3 Replies

4. Shell Programming and Scripting

How to put for loop in nawk

Hello All, How i can put loop in nawk. what i want is that i define a variable which contain vlaues like var='1 2 3 4 5' and then define for loop which gives vlaue to nawk one by one to varilable inside nawk and then print it. cat /omp-data/logs/5etr/081121.APX | nawk -v CDNLIST='700 701' ' ... (1 Reply)
Discussion started by: wakhan
1 Replies

5. Shell Programming and Scripting

Error running nawk script

Hi, I was trying to use nawk script given in this link https://www.unix.com/aix/19979-df-output-not-aligned.html but when i do this im getting this error $ df -k|formatDF.nawk -ksh: formatDF.nawk: not found Can anyone help me on this... (6 Replies)
Discussion started by: niteesh_!7
6 Replies

6. Shell Programming and Scripting

Nawk Script not working

Any idea why this isn't working? YESTERF=`TZ=aaa24 date +%b"-"%d | sed 's/-0/--/'` filelist2=$(find /export/home/gen/cks/traces \( -name \*YESTERF\* -name \*DNA\* \) -print | tr '\n' ' ') print "Date/Time,Location,Shelf,IP,Reason,Log Filename" >> $OUTPUT nawk -F':' ' $2 ~... (2 Replies)
Discussion started by: ther2000
2 Replies

7. Shell Programming and Scripting

Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example: ------------ #!/bin/ksh read date nawk -F, '{if($1==date) print $4" "$5}' ------------- Is there a way to pass an argument to nawk from shell script. Many thanks... (8 Replies)
Discussion started by: samer.odeh
8 Replies

8. Shell Programming and Scripting

How to pass nawk variable to shell within the same script?

Hi All, I tried googling but so far no luck, can someone tell me how pass the variable value used inside the nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ") but the printf which is outside the nawk command doesn't print the value or it... (4 Replies)
Discussion started by: Optimus81
4 Replies

9. 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

10. Shell Programming and Scripting

Use of -r, grep, nawk in script

Hello Friends, I am new to unix shell, need to understand the meaning of few cmds in below script, #!/usr/bin/ksh FTP_FILE_NAME="$1" if then grep "TRLR@@@@@@" $FTP_FILE_NAME | nawk -F"" '{print $2}' else echo "0" fi what is the use of -r, grep, nawk & -F in above script. Why... (2 Replies)
Discussion started by: DK2014
2 Replies
Poet::Cache(3pm)					User Contributed Perl Documentation					  Poet::Cache(3pm)

NAME
Poet::Cache -- Poet caching with CHI SYNOPSIS
# In a conf file... cache: defaults: driver: Memcached servers: ["10.0.0.15:11211", "10.0.0.15:11212"] # In a script... use Poet::Script qw($cache); # In a module... use Poet qw($cache); # In a component... my $cache = $m->cache; # For an arbitrary namespace... my $cache = Poet::Cache->new(namespace => 'Some::Namespace') # then... my $customer = $cache->get($name); if ( !defined $customer ) { $customer = get_customer_from_db($name); $cache->set( $name, $customer, "10 minutes" ); } my $customer2 = $cache->compute($name2, "10 minutes", sub { get_customer_from_db($name2) }); DESCRIPTION
Poet::Cache is a subclass of CHI. CHI provides a unified caching API over a variety of storage backends, such as memory, plain files, memory mapped files, memcached, and DBI. Each package and Mason component uses its own CHI namespace so that caches remain separate. CONFIGURATION
The Poet configuration entry 'cache', if any, will be passed to Poet::Cache->config(). This can go in any Poet conf file, e.g. "local.cfg" or "global/cache.cfg". Here's a simple configuration that caches everything to files under "data/cache". This is also the default if no configuration is present. cache: defaults: driver: File root_dir: ${root}/data/cache Here's a more involved configuration that defines several "storage types" and assigns each namespace a storage type. cache: defaults: expires_variance: 0.2 storage: file: driver: File root_dir: ${root}/data/cache memcached: driver: Memcached servers: ["10.0.0.15:11211", "10.0.0.15:11212"] compress_threshold: 4096 namespace: /some/component: { storage: file, expires_in: 5min } /some/other/component: { storage: memcached, expires_in: 1h } Some::Library: { storage: memcached, expires_in: 10min } Given the configuration above, and the code package Some::Library; use Poet qw($cache); this $cache will be created with properties driver: Memcached servers: ["10.0.0.15:11211", "10.0.0.15:11212"] compress_threshold: 4096 expires_in: 10min USAGE
Obtaining cache handle o In a script (namespace will be 'main'): use Poet::Script qw($cache); o In a module "MyApp::Foo" (namespace will be 'MyApp::Foo'): use Poet qw($cache); o In a component "/foo/bar" (namespace will be '/foo/bar'): my $cache = $m->cache; o Manually for an arbitrary namespace: my $cache = Poet::Cache->new(namespace => 'Some::Namespace'); # or my $cache = MyApp::Cache->new(category => 'Some::Namespace'); Using cache handle my $customer = $cache->get($name); if ( !defined $customer ) { $customer = get_customer_from_db($name); $cache->set( $name, $customer, "10 minutes" ); } my $customer2 = $cache->compute($name2, "10 minutes", sub { get_customer_from_db($name2) }); See CHI and Mason::Plugin::Cache for more details. MODIFIABLE METHODS
These methods are not intended to be called externally, but may be useful to override or modify with method modifiers in subclasses. initialize_caching Called once when the Poet environment is initialized. By default, calls "__PACKAGE__->config" with the configuration entry 'cache'. SEE ALSO
Poet AUTHOR
Jonathan Swartz <swartz@pobox.com> COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Jonathan Swartz. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06-05 Poet::Cache(3pm)
All times are GMT -4. The time now is 08:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy