Sponsored Content
Top Forums UNIX for Dummies Questions & Answers awk display the match and 2 lines after the match is found. Post 302595335 by eurouno on Thursday 2nd of February 2012 08:10:06 PM
Old 02-02-2012
awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file.
For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt
I tried with grep -A but it's not supported on my system.
I tried with awk, but I am not getting the first line displayed.

Here is myfile1.txt


**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 123 terminated
**ID PIN 12345
Comamnd Successful
Command Terminated
Command Successful
Command Terminated
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 345 terminated
**ID PIN 67890
Comamnd Successful
Command Terminated
Command Successful
Command Terminated
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 678 terminated
**ID PIN 13579
Comamnd Successful
Command Terminated
Command Successful
Command Terminated


After I ran awk 'c-->3;/Error/{c=5}' myfile1.txt > myfile2.txt i get this

**Port 123 terminated
**ID PIN 12345
**Port 345 terminated
**ID PIN 67890
**Port 678 terminated
**ID PIN 13579

I want these lines to be displayed, but I want the line with the error in it, too.

so myfile2.txt should look like this:


**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 123 terminated
**ID PIN 12345
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 345 terminated
**ID PIN 67890
**My Program Erorr ddmmyy hh:mm:ss My Program Error
**Port 678 terminated
**ID PIN 13579

What am I doing wrong?

Thank you
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

2. UNIX for Dummies Questions & Answers

Display n lines before match found

I have a file with following data A B C D E F G H I K L M N and search pattern is G Expected output (3 Replies)
Discussion started by: nsuresh316
3 Replies

3. UNIX for Dummies Questions & Answers

Display n lines after match found and other line

I have a file like this DoctorName Address1 Address2 DOB InsuredName Address1 Address2 DOB PatientName Address1 Address2 DOB ClaimNo1 DoctorName Address1 Address2 DOB InsuredName (2 Replies)
Discussion started by: nsuresh316
2 Replies

4. UNIX for Dummies Questions & Answers

Display lines after match is found within specified range

I have a file which has collection of segments occuring n(For eg.100) times ISA GS ST NM1*85 N3 N4 NM1*IL N3 N4 REF*D9*1001 ISE GE SE ISA GS ST NM1*85 N3 (13 Replies)
Discussion started by: nsuresh316
13 Replies

5. Shell Programming and Scripting

awk Help -- If match found return the count

Hi All, I need to get the count of records in the file, if the passing parameter matches with the list of records in the file. Below is my example source file: Test1.dat 20120913 20120913 20120912 20120912 20120912 20120912 20120912 20120913 20120913 20120912 In my script I am... (5 Replies)
Discussion started by: bbc17484
5 Replies

6. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

7. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

8. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

9. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

10. Shell Programming and Scripting

awk to calculate fields only if match is found

Trying to combine the matching $5 values between file1 and file2. If a match is found then the last $6 value in the match and the sum of $7 are outputted to a new file. The awk below I hope is a good start. Thank you :). file1 chr12 9221325 9221448 chr12:9221325-9221448 A2M 1... (5 Replies)
Discussion started by: cmccabe
5 Replies
ldapfilter.conf(4)						   File Formats 						ldapfilter.conf(4)

NAME
ldapfilter.conf - configuration file for LDAP filtering routines SYNOPSIS
/etc/opt/SUNWconn/ldap/current/ldapfilter.conf DESCRIPTION
The ldapfilter.conf file contains information used by the LDAP filtering routines. Blank lines and lines that begin with a hash character (#) are treated as comments and ignored. The configuration information consists of lines that contain one to five tokens. Tokens are separated by white space, and double quotes can be used to include white space inside a token. The file consists of a sequence of one or more filter sets. A filter set begins with a line containing a single token called a tag. The filter set consists of a sequence of one or more filter lists. The first line in a filter list must contain four or five tokens: the value pattern, the delimiter list, a filtertemplate, a match description, and an optional search scope. The value pattern is a regular expression that is matched against the value passed to the LDAP library call to select the filter list. The delimiter list is a list of the characters (in the form of a single string) that can be used to break the value into distinct words. The filter template is used to construct an LDAP filter (see description below) The match description is returned to the caller along with a filter as a piece of text that can be used to describe the sort of LDAP search that took place. It should correctly compete both of the following phrases: "One match description match was found for ..." and "Three match description matches were found for...." The search scope is optional, and should be one of base, onelevel, or subtree. If search scope is not provided, the default is subtree. The remaining lines of the filter list should contain two or three tokens, a filter template, a match description and an optional search scope. The filter template is similar in concept to a printf(3C) style format string. Everything is taken literally except for the character sequences: %v Substitute the entire value string in place of the %v. %v$ Substitute the last word in this field. %vN Substitute word N in this field (where N is a single digit 1-9). Words are numbered from left to right within the value starting at 1. %vM-N Substitute the indicated sequence of words where M and N are both single digits 1-9. %vN- Substitute word N through the last word in value where N is again a single digit 1-9. EXAMPLES
Example 1: An LDAP Filter Configuration File The following LDAP filter configuration file contains two filter sets, example1 and example2 onelevel, each of which contains four filter lists. # ldap filter file # example1 "=" " " "%v" "arbitrary filter" "[0-9][0-9-]*" " " "(telephoneNumber=*%v)" "phone number" "@" " " "(mail=%v)" "email address" "^.[. _].*" ". _" "(cn=%v1* %v2-)" "first initial" ".*[. _].$" ". _" "(cn=%v1-*)" "last initial" "[. _]" ". _" "(|(sn=%v1-)(cn=%v1-))" "exact" "(|(sn~=%v1-)(cn~=%v1-))" "approximate" ".*" ". " "(|(cn=%v1)(sn=%v1)(uid=%v1))" "exact" "(|(cn~=%v1)(sn~=%v1))" "approximate" "example2 onelevel" "^..$" " " "(|(o=%v)(c=%v)(l=%v)(co=%v))" "exact" "onelevel" "(|(o~=%v)(c~=%v)(l~=%v)(co~=%v))" "approximate" "onelevel" " " " " "(|(o=%v)(l=%v)(co=%v)" "exact" "onelevel" "(|(o~=%v)(l~=%v)(co~=%v)" "approximate" "onelevel" "." " " "(associatedDomain=%v)" "exact" "onelevel" ".*" " " "(|(o=%v)(l=%v)(co=%v)" "exact" "onelevel" "(|(o~=%v)(l~=%v)(co~=%v)" "approximate" "onelevel" ATTRIBUTES
See attributes(5) for a description of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWlldap | +-----------------------------+-----------------------------+ |Stability Level |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
ldap_getfilter(3LDAP), ldap_ufn(3LDAP), attributes(5) SunOS 5.10 9 Jul 2003 ldapfilter.conf(4)
All times are GMT -4. The time now is 07:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy