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
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
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
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
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
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
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
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
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
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
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
BTPIN(1) BSD General Commands Manual BTPIN(1)
NAME
btpin -- Bluetooth PIN utility
SYNOPSIS
btpin [-P] [-d device] [-s path] {-p pin | -r [-l len]} -a address
DESCRIPTION
The btpin utility is used to register a temporary PIN with the bthcid(8) daemon for the purposes of pairing Bluetooth devices. The PIN will
be valid for 5 minutes or until used, whichever comes first.
The options are as follows:
-a address
Specify the remote device address. The address can be specified as BD_ADDR or a name. If a name was specified, the btpin utility
attempts to resolve the name via bt_gethostbyname(3).
-d device
Specify the local device address. The device can be specified by BD_ADDR or device name. See btconfig(8) for a list of devices
available. If no device is specified, the PIN will be valid for any local device.
-l len Specify length of PIN to generate, where 1 <= len <= 16.
-P Pair with remote device. btpin will attempt to open an authenticated L2CAP connection to the Service Discovery Service on the remote
device, to force immediate pairing.
-p pin The PIN to register. The PIN may be up to 16 bytes in length.
-r Generate a random PIN, the default length is 4 bytes.
-s path
Specify path to the control socket. The default path is /var/run/bthcid.
EXIT STATUS
The btpin utility exits 0 on success, and >0 if an error occurs.
FILES
/var/run/bthcid
SEE ALSO
btkey(1), btconfig(8), bthcid(8)
AUTHORS
Iain Hibbert for Itronix, Inc
BSD
May 16, 2009 BSD