Sponsored Content
Top Forums Shell Programming and Scripting ksh passing to awk multiple dyanamic variables awk -v Post 302706979 by highnthemnts on Thursday 27th of September 2012 08:08:00 PM
Old 09-27-2012
ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded.
It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used.
ie, scan lines 45 - 20000 first pass. second pass, scan lines 104-1065,third pass scan 742-5954...

The script returns some maths: (max, min, and ::: not yet there: line numbers of max, min, block average). There are ~10,000 files to examine. would like to pass this back into an array or list variable

the $item file looks like this:
123456788,-111.2316547
123456789,-25.1234579
...

Problem: it needs to search run-time defined blocks of the file, ie, between lines r and j. I can't quite get this part to work:


it uses -v to capture the variables: awk -v rstart=$r -v jend
and tries to block it off this way: if ((NR >= $rstart) && (NR <= $jend)

Good news, the awk works if you: if ((NR >= 45) && (NR <= 1000))

Secondary issue: have the results populate an array or list that i can work with later in ksh.


Code:
function return_minmax_r_j
 {
 awk -v rstart=$r -v jend=$j 'function  max(x){i=-999;for(val in x){if(i<=x[val]){i=x[val];}}return  i;}function min(x){i=max(x);for(val in  x){if(i>x[val]){i=x[val];}}return i;}{FS=","}{if ((NR >= $rstart)  && (NR <= $jend))  a[$2]=$2;next}END{minimum=min(a);maximum=max(a);  print maximum minimum}' $item 
 }

Code:
#===MAIN===
 generate_file_name_array  #generates a list of file names

 for item in ${FILE_LIST_ARRAY[@]} 
                do 
                r=45
                j=2000  #these will be variables declared later.

                return_minmax_r_j
                done
 return

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies

2. Shell Programming and Scripting

Passing Variables to Awk

Hi I have a unix shell script with an awk statement. I would like to print some of the fields of an input file. However, I would like to print them dynamically, ie by passing the literal $1 $3 into the script to define the output. I have tried the following: variable1='$1' awk... (2 Replies)
Discussion started by: Bab00shka
2 Replies

3. Shell Programming and Scripting

Passing awk Variables

I am trying to pass the results from a variable gathered from awk, however when I echo the 'PARSE' and 'SUB', the response is blank. This is my command. awk -F= '/Unit/''{ PARSE=substr($2,1,5) ; SUB=substr($2,1,1) }' inputfile.lst Is this a kind of valid attempt or am I obligated to declare... (3 Replies)
Discussion started by: gozer13
3 Replies

4. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies

5. Shell Programming and Scripting

awk splits file in to multiple - how to get filenames in variables withing ksh?

Hi all, I'm using awk in a .ksh script to split one file by line prefix into different files (up to 4). The files are named after the prefix in the line, and a sequence no. Is there any way to get the filenames in to variables too? I need _ftpfile1, _ftpfile2, _ftpfile3 and _ftpfile4 which are... (2 Replies)
Discussion started by: spidermike
2 Replies

6. Shell Programming and Scripting

Passing awk variables to shell

Hi. I need to parse file and assign some values to variables, right now i do like below MYHOMEDIR=`awk '/Home/ {print $NF}' output.txt` MYSHELL=`awk '/Shell/ {print $NF}' output.txt` PRGRP=`awk '/Primary/ {print $NF}' output.txt` SECGRP=`awk '/Second/ {print $NF}' output.txt` In this... (10 Replies)
Discussion started by: urello
10 Replies

7. Shell Programming and Scripting

Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working. awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2' BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; } ... (4 Replies)
Discussion started by: Boomn4x4
4 Replies

8. Shell Programming and Scripting

Passing variables to awk

Hi guys, I need to fetch data from logfile between two given dates,i got the below code from our forum.It works perfect,but i need to enter the value dynamically to awk while running. awk '/2012 Jun/{p=1}!/2012 Jul/ && prev~/2012 Jul/ && p{p=0}{prev=$0}p' file i tried the below code,but... (4 Replies)
Discussion started by: mohanalakshmi
4 Replies

9. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

10. Shell Programming and Scripting

awk - passing variables in and out

Am looking to pass some Linux environment variables into AWK , can I simply use the -v option ? awk -F: -v AHOME=$HOME '{ if {rm AHOME/file.txt a=2 } }' config.txt ... (4 Replies)
Discussion started by: alldbest
4 Replies
RSTART(1)						      General Commands Manual							 RSTART(1)

NAME
rstart - a sample implementation of a Remote Start client SYNOPSIS
rstart [-c context] [-g] [-l username] [-v] hostname command args ... DESCRIPTION
Rstart is a simple implementation of a Remote Start client as defined in "A Flexible Remote Execution Protocol Based on rsh". It uses rsh as its underlying remote execution mechanism. OPTIONS
-c context This option specifies the context in which the command is to be run. A context specifies a general environment the program is to be run in. The details of this environment are host-specific; the intent is that the client need not know how the environment must be configured. If omitted, the context defaults to X. This should be suitable for running X programs from the host's "usual" X installation. -g Interprets command as a generic command, as discussed in the protocol document. This is intended to allow common applications to be invoked without knowing what they are called on the remote system. Currently, the only generic commands defined are Terminal, LoadMonitor, ListContexts, and ListGenericCommands. -l username This option is passed to the underlying rsh; it requests that the command be run as the specified user. -v This option requests that rstart be verbose in its operation. Without this option, rstart discards output from the remote's rstart helper, and directs the rstart helper to detach the program from the rsh connection used to start it. With this option, responses from the helper are displayed and the resulting program is not detached from the connection. NOTES
This is a trivial implementation. Far more sophisticated implementations are possible and should be developed. Error handling is nonexistent. Without -v, error reports from the remote are discarded silently. With -v, error reports are displayed. The $DISPLAY environment variable is passed. If it starts with a colon, the local hostname is prepended. The local domain name should be appended to unqualified host names, but isn't. The $SESSION_MANAGER environment variable should be passed, but isn't. X11 authority information is passed for the current display. ICE authority information should be passed, but isn't. It isn't completely clear how rstart should select what ICE authority information to pass. Even without -v, the sample rstart helper will leave a shell waiting for the program to complete. This causes no real harm and consumes relatively few resources, but if it is undesirable it can be avoided by explicitly specifying the "exec" command to the shell, eg rstart somehost exec xterm This is obviously dependent on the command interpreter being used on the remote system; the example given will work for the Bourne and C shells. SEE ALSO
rstartd(1), rsh(1), A Flexible Remote Execution Protocol Based on rsh AUTHOR
Jordan Brown, Quarterdeck Office Systems X Version 11 rstart 1.0.4 RSTART(1)
All times are GMT -4. The time now is 02:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy