Visit Our UNIX and Linux User Community


awk getline to fetch output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk getline to fetch output
# 1  
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 10:27 AM.. Reason: added code tags
# 2  
Old 07-22-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 02:29 PM ---------- Previous update was at 02:26 PM ----------

Could you please post an example of the desired output?
# 3  
Old 07-22-2009
Thanks a lot.

Code:
The Desired Output files are as below:
C0
C1

Values in C0
86
86
86
sum is 258 total entries 3
avg is 86

Similarly for C1

# 4  
Old 07-22-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END {
  for (Cpu in cpu) {
    outfile = "C" Cpu
    printf "Values in C%s\n%s\n", Cpu, cpu[Cpu] > outfile
    printf "Sum is %d total entries %d\n", \
    sum[Cpu], count[Cpu] > outfile 
    printf "Avg is %s\n", \
    sum[Cpu] ? sum[Cpu] / count[Cpu] : "N/A" > outfile
    close(outfile)
    }
  }    
/^ *[0-9]/ {
  cpu[$1] = cpu[$1] ? cpu[$1] RS $NF : $NF
  sum[$1] += $NF; count[$1]++
  }' infile



---------- Post updated at 03:21 PM ---------- Previous update was at 02:57 PM ----------

Actually it should be 0 instead of "N/A".

Previous Thread | Next Thread
Test Your Knowledge in Computers #619
Difficulty: Medium
In Python, variables may not be used by just assigning them a value. Declaration and data type definition is required in Python.
True or False?

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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

Featured Tech Videos