Grepping multiple lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping multiple lines in a file
# 1  
Old 04-07-2013
Grepping multiple lines in a file

HI

I have a file with output as
Code:
System: cu=4 ent=0.1 mode=on

cu    min    u    s    w    i
  0   500  0.1 0.3 0.5  0.1
  1   200  0.5 0.2 0.3 0.0

I need to grep the values of following column fields u, s, w and i from each row sum them up and store in a variable..Smilie

Please help..

Last edited by Priya Amaresh; 04-07-2013 at 06:36 AM..
# 2  
Old 04-07-2013
Code:
perl -ne '@data=split/\s+/,$_ if/^\s*\d/;$u_tot+=$data[2];$s_tot+=$data[3];$w_tot+=$data[3];END{print "Total user: $u_tot\nTotal system: $s_tot\nTotal wait: $w_tot\n";}' cpu_stat.txt

# 3  
Old 04-07-2013
Code:
$ awk '{u+=$3;s+=$4;w+=$5;i+=$6; $1=$1}; 1; END {print "total:\t", u,s,w,i}' OFS="\t" file
System: cu=4    ent=0.1 mode=on

cu      min     u       s       w       i
0       500     0.1     0.3     0.5     0.1
1       200     0.5     0.2     0.3     0.0
total:          0.6     0.5     0.8     0.1

@Skrynesaver: you must have overlooked a shift in the data array indices: use 3, 4, and 5 instead of above.
# 4  
Old 04-07-2013
Quote:
Originally Posted by RudiC
@Skrynesaver: you must have overlooked a shift in the data array indices: use 3, 4, and 5 instead of above.
In Perl, arrays are indexed from 0. There are 3 problems in the one-liner:
  1. since a regex is specified for split, it does not handle the leading white-spaces properly: let the default behaviour prevail.
  2. $w_tot+=$data[3] should be $w_tot+=$data[4]
  3. if blank lines or lines not starting with a digit character turn up between the "data" lines, one will end up with wrong totals.

A better one would be:
Code:
perl -ne '@data=/^\s*\d/?split:();$u_tot+=$data[2];$s_tot+=$data[3];$w_tot+=$data[4];END{print "Total user: $u_tot\nTotal system: $s_tot\nTotal wait: $w_tot\n"}' file

Actually, it's best to sum up only in case of "data" lines.

Last edited by elixir_sinari; 04-07-2013 at 09:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

2. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

4. Shell Programming and Scripting

Printing next two lines from a file after grepping a specific pattern

Hi I have a file like # vi require.txt 1,BANK,Read blocks that cycle. yellow Read blocks. 2,ACCOUNT,Finished Red Finished . 3,LOAN, pipe white pipe 4,PROFIT,Resolve. black Resolve Am using like cat require.txt | grep -w ACCOUNTThe output I get is (8 Replies)
Discussion started by: Priya Amaresh
8 Replies

5. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Programming

Grepping a column from multiple file

I have 20 files that look pretty much like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (5 Replies)
Discussion started by: kayak
5 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Shell Programming and Scripting

grepping all lines of one file from another file

Hi First post on here hope you can help with something I have a file with a couple of thousand lines (all lines are one string long, i.e a number I have another file that is over 1 million lines long Example entry from file 1 123456 Example from file 2 123456 mjhyuihbn ... (5 Replies)
Discussion started by: nampahc
5 Replies

10. UNIX for Advanced & Expert Users

grepping lines out of files

Hi, I wonder if anyone can help me. I have a file with about 200 lines in it. I am wanting to set up a Count so that it picks out each line at turn and edits the line. However i am having trouble pulling out the specific line. I have a feeling it will be done somehow by a grep -n but what ever i... (2 Replies)
Discussion started by: mariner
2 Replies
Login or Register to Ask a Question