awk - sed :Help Getting next lines data .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - sed :Help Getting next lines data .
# 1  
Old 01-24-2013
awk - sed :Help Getting next lines data .

Experts,
Can you please help how to get the output that are written just below "bad"

Code:
calls                   badcalls                nullrecv                
439486                  54                      0                       
badlen                  xdrcall                 dupchecks               
0                       54                      0                       
dupreqs                 
0                       

Client rpc:
Connection oriented:
calls                   badcalls                badxids                 
311459905               1473                    387                     
timeouts                newcreds                badverfs                
1423                    0                       0                       
timers                  cantconn                nomem                   
0                       0                       0                       
interrupts              
50                      
Connectionless oriented:
calls                   badcalls                retrans                 
985342                  0                       0                       
badxids                 timeouts                newcreds                
0                       0                       0                       
badverfs                timers                  nomem                   
0                       0                       0




The output should be like this:

Code:
badcalls 54 | badlen 0 | badcalls 1473 | badxids 387


Thanks a lot,
# 2  
Old 01-24-2013
Quote:
Originally Posted by rveri
Experts,
Can you please help how to get the output that are written just below "bad"

Code:
calls                   badcalls                nullrecv                
439486                  54                      0                       
badlen                  xdrcall                 dupchecks               
0                       54                      0                       
dupreqs                 
0                       

Client rpc:
Connection oriented:
calls                   badcalls                badxids                 
311459905               1473                    387                     
timeouts                newcreds                badverfs                
1423                    0                       0                       
timers                  cantconn                nomem                   
0                       0                       0                       
interrupts              
50                      
Connectionless oriented:
calls                   badcalls                retrans                 
985342                  0                       0                       
badxids                 timeouts                newcreds                
0                       0                       0                       
badverfs                timers                  nomem                   
0                       0                       0




The output should be like this:

Code:
badcalls 54 | badlen 0 | badcalls 1473 | badxids 387


Thanks a lot,
Your input file has "badcalls" 3 times, your output has 2; "badlen" 1, 1; "badxids" 2, 1; "badverfs" 2, 0. How do you decide which "bad" entries are to be counted and which are to be ignored?
# 3  
Old 01-24-2013
Don, Thanks,
I am looking for all the bad entries in the output with the values,

In more detail, all bad entries to be taken, nothing to ignore, to be look like this:

Code:
badcalls 54 | badlen 0 | badcalls 1473 | badxids 387 | badverfs 0 | badcalls 0 | badxids 0 | badverfs 0

Thanks,
# 4  
Old 01-24-2013
try:
Code:
awk '
/^bad/ || /[ \t]bad/ {
  nf=NF; cl=$0; getline nl; $0=cl " " nl;
  for (i=1; i<=nf; i++) {
     if ($(i) ~ /^bad/) out=out $(i) FS $(i+nf) " | ";
  }
}
END {sub("[ |]*$","",out); print out}
' infile

This User Gave Thanks to rdrtx1 For This Post:
# 5  
Old 01-24-2013
A more structured way is to rearrange the items into tuples per line, filtering off the heading and blank lines, so you can select the tuples you want. If needs or files change, it is easier to maintain.
This User Gave Thanks to DGPickett For This Post:
# 6  
Old 01-24-2013
A bit like DGPickett suggests:
Code:
awk '
  NF && !/:/{
    split($0,L)
    getline
    for(i in L) if(L[i]~/^bad/) s=s (s?" | ":x) L[i] FS $i
  } 
  END{
    print s
  }
' file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-28-2013
Or in sed:
Code:
sed -n '
  :loop
  $q
  N
  :loop2
  /^[a-z].*\n[0-9]/!{
    s/.*\n//
    b loop
   }
  s/^\([a-z]*\) *\(.*\n\)\([0-9]*\) *\(.*\)/\1 \3\
\2\4/
  P
  s/^[a-z]* [0-9]*\n//
  b loop2
 ' in_file

Find a pair of lines where the first begins with a lower letter and the econd begins with a number, decompose them one field at a time untile they are done and then resume the search. I call this a sed looper because only the first line is read by sed, all the rest are read by looping to an 'N'. The more usual sed form is a sed filter, where sed reads each line and the scripts act on it. You cannot do both in the same script, neatly, but you can pipe one sed to another.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - mixed for and if to select particular lines in a data file

Hi all, I am new to AWK and I am trying to solve a problem that is probably easy for an expert. Suppose I have the following data file input.txt: 20 35 43 20 23 54 20 62 21 20.5 43 12 20.5 33 11 20.5 89 87 21 33 20 21 22 21 21 56 87 I want to select from all lines having the... (4 Replies)
Discussion started by: naska
4 Replies

2. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

3. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

4. Shell Programming and Scripting

Removing repeating lines from a data frame (AWK)

Hey Guys! I have written a code which combines lots of files into one big file(.csv). However, each of the original files had headers on the first line, and now that I've combined the files the headers are interspersed throughout the new combined data frame. For example, throughout the data... (21 Replies)
Discussion started by: gd9629
21 Replies

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. Shell Programming and Scripting

[AWK] handeling data spread on multiple lines

Hello all, first off great forum. Now for my little problem. Using RHEL 5.4 and awk. Been doing code since a few month. So just starting. My problem is handeling data on multiple lines. { if ($1 != LASTKEY && h ~ /.*\/s_fr_/) { checkgecos( h, h ) h="" ... (2 Replies)
Discussion started by: maverick72
2 Replies

7. Shell Programming and Scripting

Manipulate lines with sed/awk

Hey All, I need to reorganize a file's text. Here is the source: host John_Doe filename "config.cfg"; hardware ethernet 98:10:3d:13:8f:98; fixed-address 10.10.10.29; } host Jane_Doe filename "config.cfg"; hardware ethernet 98:13:11:fd:5a:57; fixed-address 10.10.5.24; } host... (2 Replies)
Discussion started by: TheBigAmbulance
2 Replies

8. Shell Programming and Scripting

how to awk a data from seperate lines

Hi guys, i have a problem which im hoping you will be able to help me with. I have follwing output :- ------------------------------------------------------------------------------- NSTEP = 407000 TIME(PS) = 43059.000 TEMP(K) = 288.46 PRESS = 0.0 Etot = -2077.4322 ... (2 Replies)
Discussion started by: Mish_99
2 Replies

9. Shell Programming and Scripting

Inserting Lines between data sets using SED?

Hello all and thanks in advance! What I'm looking to do is insert a blank line, anytime the first 9 characters of a given line don't match the first 9 characters of the previous line. i.e. Convert the data set 1 45 64 89 1 89 69 235 2 89 234 67 2 56 90... (1 Reply)
Discussion started by: selkirk
1 Replies

10. Shell Programming and Scripting

awk / shell - Fix broken lines and data

Gurus, I am struggling with a issue and thought I could use some of your expertise. Need Help with this I have a flat file that has millions of records 24|john|account ~ info |56| 25|kuo|account ~ journal |58| 27|kim|account ~ journal |59| 28|San|account ~ journal |60|... (3 Replies)
Discussion started by: rimss
3 Replies
Login or Register to Ask a Question