Sponsored Content
Top Forums Shell Programming and Scripting awk find a string, print the line 2 lines below it Post 302465147 by mglenney on Thursday 21st of October 2010 05:51:37 PM
Old 10-21-2010
awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data:

Code:
define hostgroup{
    hostgroup_name    chat-dev
    alias        chat-dev
    members        thisisahostname
    }

define hostgroup{
    hostgroup_name    chat-qa
    alias        chat-qa
    members        thisisadifferenthostname
    }

I have the following one-liner which works:

Code:
$ awk -v hgn="chat-qa" '{ if ($1 == "hostgroup_name" && $2 == hgn ) { getline;getline;print $0 } }' testfile
    members        thisisadifferenthostname

Is there a better way to do this? First, the double getline seems a little cheesy to me. Second, what I'd really like to do is, once I find my string, search line by line after it until you find a line in which $1=="member" and then print that line. That way, if that line isn't exactly 2 lines later I'll still find it.

Thanks,

MG
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

2. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

3. Shell Programming and Scripting

Print lines after the search string until blank line is found

All I want is to look for the pattern in the file...If I found it at # places... I want print lines after those pattern(line) until I find a blank line. Log EXAMPLE : MT:Exception caught The following Numbers were affected: 1234 2345 2346 Error java.lang.InternalError:... (3 Replies)
Discussion started by: prash184u
3 Replies

4. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

5. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

6. Shell Programming and Scripting

Search string and print the above line and below lines?.

if the first string matches then print the previous line and current line and also print the following lines if the other string search matches. Input ------ TranTime 2012 10 12 The Record starts here Accountnumber: 4632473431274 TxnCode 323 TranID 329473242834 ccsdkcnsdncskd... (7 Replies)
Discussion started by: laknar
7 Replies

7. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

8. Shell Programming and Scripting

awk find and print next string

Can I do this in one awk session. Solution I have is poor. I want to return the number after PID. echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}' (6 Replies)
Discussion started by: u20sr
6 Replies

9. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

10. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies
ppp.Dialers(4)						     Kernel Interfaces Manual						    ppp.Dialers(4)

NAME
ppp.Dialers - PPP dialer description file format DESCRIPTION
The file describes how to dial each type of modem attached to the UNIX system that is to be made available for outbound PPP calls. exam- ines it when placing a call to a neighboring machine. When selects a line from it uses the field to select an entry in from which it uses the field to select an entry in then interprets the field from that dialer description. Format Entries are one to a line; blank lines are ignored. Comments begin with a and extend to the end of the line. Upper/lower case distinc- tions in the dialer field are significant for matching purposes, as are strings in the chat script. Fields on a line are separated by hor- izontal white space (blanks or tabs). If a chat script ends with a backslash the next line is considered a continuation of the chat script. Continuations may only occur in the midst of a chat script. Each entry must contain these fields, in this order: dialer The name of this dialer, to be matched against the dialer field in chat-script A description of the conversation that holds with the modem. Chat Script Particulars A chat script takes the form of a space-separated list of expect-send pairs. Each pair consists (at minimum) of a field to expect the end to send, then a field to send in response. Unless a string ends with will follow it by sending a carriage return character (ASCII 0x0d). Chat scripts are or where the following the hyphen is executed if the preceding fails to match received text. Certain special words may be used in the chat script to control the behavior of as it attempts to dial. Both ABORT and TIMEOUT must be in the phase of the chat script. If sees abort-string while executing the remainder of the chat script, abort the dialing attempt and note the failure in the log file. While executing the current chat script, wait timeout-time seconds for a response before considering the dialing attempt to have timed out. Writes have a fixed 60-second timeout. The expect-send couplet of 'sets the line parity accordingly: Set transmission parity based on the parity observed in characters received in strings. This is the default. Transmit characters with the parity bit set to zero (8 bits, no parity). Transmit characters with the parity bit set to one. Transmit characters with even parity. Transmit characters with odd parity. In the midst of either an string or a string, gets translated into the appropriate control character, and gets translated into x. Other special sequences are: Send or receive a space character (ASCII 0x20). Send or receive a horizontal tab character (ASCII 0x09). Send or receive a line feed character (ASCII 0x0a). Send or receive a carriage return character (ASCII 0x0d). Send or receive a backslash character (ASCII 0x5c). Send or receive a carat character (ASCII 0x5e). Send or receive the single character Ctrl-character (ASCII 0x00 through 0x1f). Send or receive a character, specified in octal digits. Pause for .25 second before proceeding (send only). Delay for two seconds before proceeding (send only). Send a break (.25 second of zero bits). Disable hangups (sets CLOCAL or LNOHANG). enable hangups (unsets CLOCAL or LNOHANG) (the default). Don't append a carriage return character after sending the preceding string (send only). Don't print succeeding send strings (e.g., a password) in any debugging or logging output. Subsequent sequences toggle mode. Insert the telephone number (found in the fifth field of here. EXAMPLES
# # Dialers - PPP dialers file # #Dialer Chat script T1600 ABORT NOsCARRIER ABORT NOsDIALTONE ABORT BUSY ABORT RRING RRING RRING ABORT ERROR TIMEOUT 5 "" AT OK-AT-OK ATS111=0DTT TIMEOUT 30 CONNECT # T2500-PEP ABORT NOsCARRIER ABORT NOsDIALTONE ABORT BUSY ABORT RRING RRING RRING ABORT ERROR TIMEOUT 5 "" AT OK-AT-OK ATS111=0DTT TIMEOUT 30 CONNECTsFAST # USRv32bis ABORT ERROR ABORT NOsANSWER ABORT NOsCARRIER ABORT BUSY ABORT RRING RRING RRING ABORT NOsDIALsTONE TIMEOUT 5 "" AT&F OK-ATQ0-OK ATB0E0X7&B1&H1&I0&K3&R2&S1 OK-AT-OK ATS01=1S02=255S19=0 OK-AT-OK ATDTT TIMEOUT 30 CONNECT AUTHOR
was developed by the Progressive Systems. SEE ALSO
pppd(1), ppp.Auth(4), ppp.Devices(4), ppp.Filter(4), ppp.Keys(4), ppp.Systems(4). RFC 1055. RFC 1144, RFC 1332, RFC 1548, ppp.Dialers(4)
All times are GMT -4. The time now is 04:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy