Match a pattern and copy above two lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match a pattern and copy above two lines
# 1  
Old 07-31-2008
Match a pattern and copy above two lines

Dear experts,

i want to search pattern tre and copy this line and above two lines
in a seperate file::

Thanks for the help

SEV="MAJOR": RX-TX [10] HW-FAILURE [239] DOMAIN="alcomc2_BSS_20
unit-type % bts nbr % 24
SBL-type % tre nbr % 4 subnb % 255


BR,
Danish
# 2  
Old 07-31-2008
Code:
grep -B2 'tre' file

Regards
# 3  
Old 07-31-2008
Help please !!!!

Dear Fellow,

Thank you very much for the reply,

This line didn't produce anything, i am using solaris 10.

i use it like this grep -B2 'tre' temp.txt > output.txt
temp.txt contains lots of alarms like this.

I want to search pattern tre and copy this line and above two lines in seperate file, and tre is present many times.


Thank a lot.
# 4  
Old 07-31-2008
Hi,

Try this ...

for i in `grep tre My.log`
do
strow=`expr $i - 2`
myline=`echo "sed -n ""${strow}p"" My.log"`
done > MyNew.log
# 5  
Old 07-31-2008
You probably want grep -n in order for that to work.
# 6  
Old 07-31-2008
Still NOK

BOSS!!!

I am getting below mentioned error message with this code
for i in `grep tre My.log`
do
strow=`expr $i - 2`
myline=`echo "sed -n ""${strow}p"" My.log"`
done > MyNew.log

Dear ERA, can you please tell me how to grep tre and above two lines with grep -n command, also i have a lot of tre pattern in this file.

Thanks in advance for all you help..
I need a little more


===========================ERROR======================
++ for i in '`grep tre help.txt`'
+++ expr SBL-type - 2
expr: non-numeric argument
++ strow=
+++ echo 'sed -n p help.txt'
++ myline='sed -n p help.txt'
++ for i in '`grep tre help.txt`'
+++ expr % - 2
expr: syntax error
++ strow=
+++ echo 'sed -n p help.txt'
++ myline='sed -n p help.txt'
++ for i in '`grep tre help.txt`'
+++ expr tre - 2
expr: non-numeric argument
++ strow=
+++ echo 'sed -n p help.txt'
++ myline='sed -n p help.txt'
++ for i in '`grep tre help.txt`'
+++ expr nbr - 2
expr: non-numeric argument
# 7  
Old 07-31-2008
Hi Danish,

Era already mentioned it use "grep -n tre ..." instead of "grep tre ..."
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

3. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

4. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

5. Shell Programming and Scripting

counting lines that match pattern

I have a file of 1.3 millions lines. some are with the same word twice on the line, some line have two diffrent words. each line has two words, one in brackets. example: foo (foo) bar (bar) thae (awvd) beladf (vswvw) I am sure this can be done with one line of... (6 Replies)
Discussion started by: robsonde
6 Replies

6. UNIX for Dummies Questions & Answers

Join the lines until next pattern match

Hi, I have a data file where data is splitted into multiple lines. And, each valid record starts with a patten date | <?xml and ends with pattern </dmm> e.g. 20120924|<?xml record 1 line1....record 1 line1....record 1 line1.... record 1 line2....record 1 line2....record 1 line2.... record 1... (3 Replies)
Discussion started by: Dipalik
3 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

9. UNIX for Dummies Questions & Answers

retrieve lines that match a pattern

Hi, I would like to know how can I get lines from a text file that match no more than 2 '>'. Example: Input file: a >cr1 4 a>b b>c a >cr2 5 a>b Output file: a >cr2 5 a>b Thanks in advance (2 Replies)
Discussion started by: fadista
2 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies
Login or Register to Ask a Question