Awk help with source and previous line loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk help with source and previous line loop
# 1  
Old 10-09-2010
Awk help with source and previous line loop

Hello,

I've written a ksh awk script to ping multiple servers and write the results to a file. That part is working ok. I then want to extract the names of only the server which are available. This is indicated by '1 packets received'. The server name actually appears above that line so I found a command that finds source data and prints the line above as well.
Anyway to cut a long story short the script is only finding the first available server and not the rest. My source file looks like:

Code:
--- serv001 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss PING serv001 (xx.xx.xxx.xx): 56 data bytes
--- serv002 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss PING serv002 (xx.xx.xxx.xx): 56 data bytes
64 bytes from xx.x.xx.xx: icmp_seq=0 ttl=254 time=0 ms
--- serv003 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max = 0/0/0 ms PING server003 (xxx.xx.xxx.xx): 56 data bytes
--- server004 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss PING server004 (xx.xx.xxx.x): 56 data bytes
--- server005 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss PING 
server005 (xx.xx.x.xx): 56 data bytes
--- server006 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss round-trip min/avg/max = 0/0/0 ms PING server006 (xx.xx.xx.xx): 56 data bytes

the code is:
Code:
cat pingy99 | awk 'NR==1{str=$0}$0~/1 packets received,/{ print str,$0}'>p77

echo The following servers are available:
cat p77


but this only shows the first server found (server3)

Sorry for such a long post but I suppose the bottom line question is:

How do I get the awk to read and report on the entire source file?

Any help appreciated

Last edited by Franklin52; 10-09-2010 at 08:47 AM.. Reason: Please use code tags
# 2  
Old 10-09-2010
Something like this?
Code:
awk '/1 packets received/{print p RS $0}{p=$0}' pingy99

# 3  
Old 10-09-2010
Cheers Franklin that worked fine.

If you get a moment could you break down the command for me. I'm still new to awk!
# 4  
Old 10-09-2010
Quote:
Originally Posted by Grueben
Cheers Franklin that worked fine.

If you get a moment could you break down the command for me. I'm still new to awk!
Sure Smilie:
Code:
awk '/1 packets received/{print p RS $0}{p=$0}'

Explanation:
Code:
/1 packets received/{print p RS $0}

If the current line matches the pattern /1 packets received/ print the previous line p a record separator and the current line. Notice that the variable p still holds the previous line.
Code:
{p=$0}

Store the current line in the variable p.
# 5  
Old 10-10-2010
Cheers really appreciate that
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to copy previous line matching a particular columns

Hello Help, 2356798 7689867 999 000 123678 20385907 9797 666 17978975 87468976 968978 98798 I am trying to have out put which actually look for the third column value of 9797 and then it insert line there after with first, second column value exactly as the previous line and replace the third... (3 Replies)
Discussion started by: Indra2011
3 Replies

2. Shell Programming and Scripting

Any solution to get previous line in awk

Hi all... Is there any solution to do the reverse process of getline ? Thanks in advance. (5 Replies)
Discussion started by: Akshay Hegde
5 Replies

3. Shell Programming and Scripting

awk to insert line previous to a pattern?

I have a very long line with certain patters embedded in there. I need to be able to read that line, and when it encounters that pattern, create a new line. I want the pattern to be the beginning of the new line. I thought sed or awk could do this, but everything I try in sed gives me a "sed... (2 Replies)
Discussion started by: Drenhead
2 Replies

4. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

5. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

6. Shell Programming and Scripting

Read column values from previous and next line using awk

Hi, I have a csv file which contains data that looks something like this: Key1 Key2 Key3 New_Key1 New_Key2 New_Key3 102 30 0 - - - 102 40 1 30 40 50 102 50 2 40 50 ... (4 Replies)
Discussion started by: Nishi_Licious
4 Replies

7. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

8. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

9. Shell Programming and Scripting

Awk script to create new file based on previous line

Need help creating a script that does the following: Sort a file Compare the previous line "last field" with current line "last field" If they are the same, print output to a file If they are different print output to a new file The script should keep creating new files if the previous... (5 Replies)
Discussion started by: Muga801
5 Replies

10. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies
Login or Register to Ask a Question