Grep for a pattern based on another patter


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep for a pattern based on another patter
# 1  
Old 10-17-2008
Grep for a pattern based on another patter

hi,

I have looked at many grep threads and am unable to find something like this: please help.
I have a file which is generated from a report generator and i am trying to load a whole lot of specific data into a table for the users. One field is causing me problems.All the rest i can manage.
I want to grep lines with date: (grep "date:" filename)..
however i want those dates only where there is a TEST in the 7th line below it and before the next pattern(dateSmilie

date: 01/01/2001
xx
yy
zz
aa
bb
cc
TEST
zz
date: 01/02/2001
xx
yy
zz
aa
bb
cc
NOTEST
xx
date: 01/03/2001
xx
yy
zz
aa
bb
cc
NOTEST
yy
date: 01/04/2001
xx
yy
zz
aa
bb
cc
TEST
aa


so the result will be :
date: 01/01/2001
date: 01/04/2001

Any help pl...

thanks
# 2  
Old 10-17-2008
here is my script... this gives me all the dates offcourse:
I remove the last few lines and look for date and cleanse based on the data and write it with the filename,
rm 45days_dates.txt
for file in `ls *.txt`
do
sed -e :a -e '$d;N;2,22ba' -e 'P;D' $file | grep "Date:" | sed 's/\\par//g' | sed 's/Date://g'| sed '/^$/d' | awk '{print $1}' > dummy.dat
while IFS=" " read line
do
print "$file" "$line" >> 45days_dates.txt
done < dummy.dat
rm dummy.dat
done
# 3  
Old 10-17-2008
Code:
awk '/^date/ { _ = $0; n = NR }
/^TEST/ && NR == n + 7 { print _ }
' infile

# 4  
Old 10-17-2008
YOU CAN USE THIS SCRIPT FOR YOUR filename
Quote:
a=1;
b=2;
no=`grep 'date' filename |wc -l |awk '{print $1}'` ;
while [ $a -le $no ]; do
bname=`grep -e 'TEST' -e 'date' filename |sed -n "$b"p` ;
if [ "$bname" == "TEST" ];then
grep 'date' raj | sed -n "$a"p ;
fi
a=`expr $a + 1 ` ;
b=`expr $b + 2 ` ;
done
# 5  
Old 10-18-2008
Thank you radoulov and rahul for your response.

I tried your script radoulov and it works just perfect.. onlt thing is i dont have he date as the first field so i removed the ^ in the pattern.

thank you so much!

Note: rahul i will try your script as well.. thanks
# 6  
Old 10-18-2008
Also i could understand the code. hope i am right.

Look for pattern date. define an array and store the whole row in it.
n=NR - record number
If pattern is TEST and NR=7 print the array.
# 7  
Old 10-18-2008
It's even simpler (the code does not use array(s)):
- if the current input record matches the pattern date - save the entire record $0 in the variable _ and save its number NR in the variable n.
- if the current input record matches the pattern TEST and its number is equal to n + 7 (7 records after the previous one), print the previously saved record _.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

5. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

6. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

7. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

8. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

grep for a particular pattern and remove few lines above top and bottom of the patter

grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern root@server1 # cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa ... (17 Replies)
Discussion started by: fed.linuxgossip
17 Replies

10. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies
Login or Register to Ask a Question