Sponsored Content
Full Discussion: awk getline to fetch output
Top Forums Shell Programming and Scripting awk getline to fetch output Post 302336462 by bhallarandeep on Wednesday 22nd of July 2009 07:56:40 AM
Old 07-22-2009
awk getline to fetch output

Input File:
Code:
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100
CPU minf mjf xcal  intr ithr  csw icsw migr smtx  srw syscl  usr sys  wt idl
  0   37   0  594  2205 1174  123    8    6   36    0  1954    6   8   0  86
  1    1   0   49    26    0   44    3    2    6    0   624    2   1   0  97
  2    0   0    5     5    0    8    1    0    1    0    89    0   0   0 100
  3    0   0    0     2    0    2    1    0    0    0    13    0   0   0 100
  4    5   0   46    18    0   33    3    2   13    0   486    2   1   0  98
  5    0   0    1     2    0    2    1    0    1    0    28    0   0   0 100

I am adding all the values in last column of CPU0 and then sending the output to C0.
For this I am using the code below:
Code:
cpu0()
{
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;cpu0[i]=$NF;sum=sum+cpu0[i];i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",cpu0[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

cpu1()
{
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;getline;cpu1[i]=$NF;sum=sum+cpu1[i];i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",cpu1[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

cpu0 $1>C0
cpu1 $1>C1

But the concern is number of CPU’s is not fixed and I was trying to implement a for loop:
cpu()
{
arrcpu[1]=0
cat $1|awk 'BEGIN{i=1;sum=0;}
/CPU/{getline;arrcpu[i]=$NF;sum=sum+arrcpu[i];i=i+1;}
END {j=1;k=1;OFS="\t"
while ( j < i)
{
printf "%s\n",arrcpu[j]
j=j+1;
}
printf "sum is %s total entries %s\n",sum,i;
printf "avg is %s\n",sum/i;
}'
} # End of function

for num in `cat cpu_num`
do
cpu $1 > C$num
done

However I am not able to increment “getline” every time the loop is executed because of which same output is returned every time. Please suggest if there is any other alternative to this problem.

Last edited by radoulov; 07-22-2009 at 09:27 AM.. Reason: added code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk:Problem with getline

$ echo |awk ' BEGIN {"date" | getline current_time;close("date");print "Report printed on " current_time}' Report printed on Thu May 11 14:57:29 METDST 2006 This example works fine but how can i print all the output when is longer... (3 Replies)
Discussion started by: Klashxx
3 Replies

2. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies

3. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

4. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Unable to fetch the output using AWK

Hi all, From the below file I need to fetch the data in the below output format. ToolInstanceID "diw_dm_sales" Identifier "Sales_source_load" Promt Default ParamType ParamLength ParamScale "Database_Name" "ORCL" "0" "0" "0" Identifier "retail_source_load" Promt Default... (4 Replies)
Discussion started by: onesuri
4 Replies

6. Shell Programming and Scripting

Some Awk Getline help?

Greetings, I have about 3000 files that I want to search. The first column in all of these 3000 files has a unique serial number on each line. The subsequent columns have lots of data. I have another masterfile with three columns to help me find all the data I need in a moments notice: col 1... (15 Replies)
Discussion started by: jeeplou
15 Replies

7. Shell Programming and Scripting

awk getline problem

Hello, I want to print out the DNA sequence entries (tens of thousand!) that are longer than certain value (i=200) from a file (FASTA file) as: >S94D_ctg_8004 Average coverage: 402.95 ATAATGCCTGTGAATATGACATGTGTTCCTGTTTCTACATCAGACTACTATTCTTGCATA... (12 Replies)
Discussion started by: yifangt
12 Replies

8. Shell Programming and Scripting

awk getline function help

Hello All , I have to split a file as well as keep the header in all the splitted files. For this I am using the getline function of awk to keep the header however the catch is header is of 4 lines and I have to hold all the 4 lines by getline function(or is there any other option ???) into a... (5 Replies)
Discussion started by: Pratik4891
5 Replies

9. Shell Programming and Scripting

awk getline

Hi, I have an awk script with the following function in it . function cmd( c ) { while( ( c | getline foo) > 0 ){ return foo ; close( c ); } } c =... (4 Replies)
Discussion started by: MetaMan
4 Replies

10. Shell Programming and Scripting

awk with if, getline, and another if

Howdy Folks, It seems like it is always awk that confuses the heck out of me and I even have books and examples. I have this line: awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt to parse this text: <api... (4 Replies)
Discussion started by: port43
4 Replies
All times are GMT -4. The time now is 06:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy