Lines of code in egrep question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Lines of code in egrep question
# 1  
Old 01-22-2015
Lines of code in egrep question

Hi,
I have a question, during my readings it appears that these two variables in the snippet below need to be on the same line to return a “true” answer and listed in the output otherwise it won’t be returned. How can I write this snippet to make it return a “true” if those two variables are on two separate lines in the trace file?? How would this piece of the code be written?
Code:
egrep  -ce  "($2015-01-22.*$NS Primary Error) | ($NS Primary Error.*$2015-01-22)" /oracle/diag/rdbms/musidp/musidp/trace/musidp_d000_21750.trc

Or am I misunderstanding something here?
Thanks in advance.

Last edited by Corona688; 01-22-2015 at 04:04 PM.. Reason: Code tags, not font tags
# 2  
Old 01-22-2015
is your $NS variable set?
Or better yet... what's this $2015-01-22? Looks like a variable called 2015-01-22
Shouldn't this simply be:
Code:
egrep  -ce  '(2015-01-22|NS Primary Error)' /oracle/diag/rdbms/musidp/musidp/trace/musidp_d000_21750.trc

# 3  
Old 01-22-2015
First off, your regex is a little strange. "$2015-01-22" is nonsensical because $ means "match the end of the line", and there's nothing after the end of the line. Did you mean ^, "match the beginning of the line"? It will also take your spaces literally, so "(...) | (...)" means "match this block and a space, or match a space then this block".

But anyways, grep can't can't remember what happened in past lines. It's not a programming language.

awk can remember what's happened in past lines, because it is.

Code:
awk '/2015-01-22.*NS Primary Error/ { A=1 } /NS Primary Error.*2015-01-22/ { B=1 } END { exit(!(A && B)); }' inputfile

If it finds lines matching "2015-01-22.*NS Primary Error" it sets the variable A true. If it finds lines matching "NS Primary Error.*2015-01-22" it sets the variable B true. If, once all lines are read, both A and B are true, it returns 0(i.e. success), otherwise returns 1 (failure) to the shell.
# 4  
Old 01-22-2015
You might list the lines of each pattern in different work files a and b using 2 egrep patterns/passes, and then sort each (comm needs a binary not ascii-numeric sort) and use 'comm' -3 to remove the same-line entries. What comes out is either a file lines or b file lines prefixed with a tab.
# 5  
Old 01-22-2015
Hi Vgers
Have to admit I am really new at this and am trying to learn by reading and doing.

The code that I listed was wrong I was just trying something. I tried your code and it returned a number (29). I went into the file to verify if it returned the right number but it didn’t. I counted 5 2015-01-22 instances of this search string and 24 instances of the NS primary error string. See output below.
What I need to have is to return true only that number that has BOTH the date and the NS primary error string which the answer should be 5.
I am thinking that there should be two egreps on for 2015-01-22 and then egrep for NS primary error.
Here is the data that egrep is looking for together (highlighted) snippet from trace file
Code:
*** 2015-01-22 17:13:33.267
unexpected error 12560 for connection:
NS Primary Error: TNS-12535: TNSperation timed out
NS Secondary Error: TNS-12560: TNSrotocol adapter error
NT Generic Error: TNS-00505: Operation timed out

Both of these highlighted must return true to be counted.
My updated script from your suggestion does this:
Code:
egrep -e '.*2015-01-22' /oracle/diag/rdbms/musidp/musidp/trace/musidp_d000_21750.trc | egrep -ce 'NS Primary Error' '{}' '+'

The first half of the script returns the following so when that info gets to the second egrep it will always return zero.
RETURNS THE FOLLOWING OUTPUT:
Code:
2015-01-22 11:21:29
2015-01-22 11:29:31
2015-01-22 11:32:01
2015-01-22 11:34:22
2015-01-22 11:35:16

Right now I am stumped. I hope this additional information helps.
Thanks in advance
al

---------- Post updated at 07:03 PM ---------- Previous update was at 06:43 PM ----------

Hi Corona,

Thanks for taking a look at this for me. I am really new at this and am currently learning on the job.

I ran your statement on the server and it returned the following error:

Code:
awk: syntax error near line 1
awk: illegal statement near line 1

I ran it with just one trace file with known information in it. In the end though I will need to go through the entire directly of trace files to find the appropriate ones.

Regards,
al

Last edited by Scrutinizer; 01-23-2015 at 01:14 AM.. Reason: Rooted out formatting, introduced code tags
# 6  
Old 01-23-2015
On Solaris use /usr/xpg4/bin/awk rather than awk

--
Your egrep approach will not work, because it is line based. The syntax of the second part is incorrect too and it the regular expression on the first part can be reduced to
Code:
egrep 2015-01-22

In the first example you use a -c option which counts the number of appearances. But now it is the actual result you are after?

Please provide a corresponding input sample and output sample and also verbally describe what result you are trying to achieve. It also helps if you state your specific operating system and version. I guessed it this time from the error message you reported....

---
Also, to find al matches, try:
Code:
/usr/xpg4/bin/awk -v ts="2015-01-22" '/^\*\*\*/ {if($0~ts) p=$0; else p=x} p!="" && /Primary Error/{print p ORS $0}' file

Or, to combine this with count and exit status, like Corona688 suggested, try something a bit more elaborate:
Code:
/usr/xpg4/bin/awk -v ts="2015-01-22" '
  /^\*\*\*/ {
    if($0~ts) p=$0
    else p=x
  } 
  p!="" && /Primary Error/ {
    print p ORS $0
    c++
  } 
  END { 
    print c+0 " Matches found"
      if(c==0) exit 1
  }
' infile


Last edited by Scrutinizer; 01-23-2015 at 01:55 AM..
# 7  
Old 01-23-2015
Yes, even 'sed' is more capable of dealing with multiple lines, using 'N' to pile up the entre block for testing, perhaps removing all but interblock line feeds.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies

2. Shell Programming and Scripting

sed and egrep question

Its really 2 questions, but both are pretty basic. Linux Redhat 1. Need to do a search and replace on a file. I need to append '--' (comment out the line) to specific lines based on a wildcard search. So if I Have GRANT SOME_ROLE_OR_USER ... I dont care what comes after that.... (2 Replies)
Discussion started by: guessingo
2 Replies

3. Shell Programming and Scripting

Including EOL in egrep pattern for multiple lines

Hi all I need your help to get a high-performance solution. I am working on a extensive script to automate file restores using the bprestore tool on a Solaris 5.10 server (bash 3.00). I will only paste the needed parts of the script to avoid any confusion. To use the script the user has to... (2 Replies)
Discussion started by: Anonym
2 Replies

4. Shell Programming and Scripting

Egrep strings on different lines in file

test.txt: appleboy orangeletter sweetdeal catracer conducivelot I want to only grep out lines that contain "appleboy" AND "sweetdeal". however, the closest thing to this that i can think of is this: cat test.txt | egrep "appleboy|sweetdeal" problem is this only searches for all... (9 Replies)
Discussion started by: SkySmart
9 Replies

5. Shell Programming and Scripting

regex question using egrep

Hi, i have a a bunch of directories that are always named with six lowercase alpha's and either one or two numeric's (but no more) so for example names could be qwerty1 qwerty9 qwerty10 qwerty67 I am currently using two pattern matches to capture these names echo $DIR |... (8 Replies)
Discussion started by: rethink
8 Replies

6. UNIX for Dummies Questions & Answers

egrep count question

Hello all, I'm a first time poster and a unix/linux noob so please be understanding. I am trying this command below: # egrep -c "Oct".+"Connect: ppp" /var/log/messages* /var/log/messages:53 /var/log/messages.1:35 /var/log/messages.2:63 /var/log/messages.3:27 /var/log/messages.4:12 ... (1 Reply)
Discussion started by: morrowtech
1 Replies

7. Shell Programming and Scripting

egrep question

I want to egrep for certain fields which are not existing in the current log files and am getting errors for that... egrep "'^20090220.14'|'^20090220.15'|'^20090220.16'|'^20090220.17'|'^20090220.18'" Some of the times are in future and logs don't have those entries and I get errors for them... (1 Reply)
Discussion started by: jacki
1 Replies

8. UNIX for Dummies Questions & Answers

Egrep question

I have a script that does the following. It searches a listing of directories with specific extensions and then formats a wc on those files. The code looks like this find <directory> -name '*.js' -o -name '*.html' | awk '{print \"wc -l \"$1}' > file \n" The result is a file with the "wc -l"... (7 Replies)
Discussion started by: mastachef
7 Replies

9. UNIX for Dummies Questions & Answers

egrep counting every 2 lines of result as 1

Hi, Can someone help me count this line: Say I have a file (file1.txt) that contains below: 11/16 13:08:19.5436 18096 --- Generating a <reading> event 11/16 13:08:19.7784 18096 ---- Sending a <writing> event 11/16 13:08:37.4516 18096 --- Generating a <reading> event 11/16... (1 Reply)
Discussion started by: Orbix
1 Replies

10. UNIX for Dummies Questions & Answers

question on egrep

Hi I am trying to use this command: egrep '^a{2,6}$' testexpr4D to retreive lines with 2,3,4,5, or 6 a's in a file . The file testexpr4D has entries like: a aa aaa aaaa aaaaa aaaaaa 123456 ABCDEF I was expecting to see 5 lines in the output but nothing happens. Can anyone help... (10 Replies)
Discussion started by: rohitv
10 Replies
Login or Register to Ask a Question