Is it possible to grep with a newline in the middle of an expression?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Is it possible to grep with a newline in the middle of an expression?
# 1  
Old 07-13-2009
Is it possible to grep with a newline in the middle of an expression?

I'm hoping that there is a way to do this. I'm sure I won't be able to get the answer I need on the deadline I need it...but at least I'll learn how to solve this problem.

I have a file that looks like this:

Code:
(00:14:25\[ddeco@nycsmitddeco)
[~/dvr]$ head -27 QNHDSPACEDVR 
Name: PollDhctAVFSInfo 00:0F:21:4B:00:6A
Name: PollDhctAVFSInfo 00:0F:21:B8:9E:D2
Name: PollDhctAVFSInfo 00:0F:21:BC:8A:30
Name: PollDhctAVFSInfo 00:14:F8:A7:39:76
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 64.4 GB (28748 clusters)
     AV Free Space: 8.0 GB (3585 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:3C:00
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 67.0 GB (29926 clusters)
     AV Free Space: 5.3 GB (2407 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:43:8E
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 45.1 GB (20164 clusters)
     AV Free Space: 27.2 GB (12169 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:4B:CC
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 49.9 GB (22296 clusters)
     AV Free Space: 22.4 GB (10037 clusters)
   AV Cluster Size: 4700

Each line that starts with "Name" indicates a piece of equipment that I ran a query on. If it has AV yadda on the lines after it, then it responded to the query, if the next line is just another Name line, it did not. The problem is that I need to remove just the equipment that did not respond to the query.

The problem is, there are 96,000 pieces of equipment in this file alone, and I need to remove the ones that didn't respond so I can format the rest into a report (whole other issue).

The only way I can think to do this in a timely manner is some variation of the following:

Code:
(00:18:35\[ddeco@nycsmitddeco)
[~/dvr]$ egrep -v "Name: PollDhctAVFSInfo 00:**:**:**:**:**\nName" QNHDSPACEDVR |head -20
Name: PollDhctAVFSInfo 00:0F:21:4B:00:6A
Name: PollDhctAVFSInfo 00:0F:21:B8:9E:D2
Name: PollDhctAVFSInfo 00:0F:21:BC:8A:30
Name: PollDhctAVFSInfo 00:14:F8:A7:39:76
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 64.4 GB (28748 clusters)
     AV Free Space: 8.0 GB (3585 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:3C:00
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 67.0 GB (29926 clusters)
     AV Free Space: 5.3 GB (2407 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:43:8E
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 45.1 GB (20164 clusters)
     AV Free Space: 27.2 GB (12169 clusters)

Basically, I want to eliminate all lines that Start with Name, end with a MAC address, and are followed on the next line by Name.

Unfortunately as you can see, that's not working out too well. I've tried many different combinations of new line characters (\n) and start line/end line characters ($/^). But I still get the results above, not what I'm looking for.

I'm currently working in cygwin on my PC at work, but I also have access to Solaris 10 (the server where the file came from) and my personal ubuntu box.

The only other way I've been able to even come close to my goal is to run the following loop (where MACSQN is all of the MACs in the file, unsorted) which leaves me with two files, good MACs and bad MACs, but as there are 96K MACs to go through...it would take way to long to be useful.

Code:
 for i in `cat MACSQN`;do if [[ `grep -A1 $i QNHDSPACEDVR|tail -1|awk '{print $1,$2}'` != "AV Size:" ]];then echo "$i"|tee -a nonQN;else echo "$i"|tee -a goodQN;fi;done

# 2  
Old 07-13-2009
Try...
Code:
awk 'function f(){if($1~/^Name:/&&p~/^Name:/)print p;p=$0}{f()}END{f()}' file1

Result...
Code:
Name: PollDhctAVFSInfo 00:0F:21:4B:00:6A
Name: PollDhctAVFSInfo 00:0F:21:B8:9E:D2
Name: PollDhctAVFSInfo 00:0F:21:BC:8A:30



---------- Post updated at 01:25 PM ---------- Previous update was at 01:21 PM ----------

Inverse...
Code:
awk 'function f(){if($1!~/^Name:/||p!~/^Name:/)print p;p=$0}{f()}END{f()}' file1

Result...
Code:
Name: PollDhctAVFSInfo 00:14:F8:A7:39:76
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 64.4 GB (28748 clusters)
     AV Free Space: 8.0 GB (3585 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:3C:00
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 67.0 GB (29926 clusters)
     AV Free Space: 5.3 GB (2407 clusters)
   AV Cluster Size: 4700
Name: PollDhctAVFSInfo 00:14:F8:A7:43:8E
           AV Size: 72.4 GB (151965100 sectors)
       AV Capacity: 72.4 GB (32333 clusters)
     AV Used Space: 45.1 GB (20164 clusters)
     AV Free Space: 27.2 GB (12169 clusters)

# 3  
Old 07-13-2009
@Ygor

I was trying to solve it with sed using hold buffer...
Can you guide how it should be done?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep expression

im trying to find the best way to match an expression using grep. input file is <html>word word word word.</a> <html>word word word word word word .</a> <html>word word word word word word word word .</a> <html>word word word word word word word word word word word word word word word word... (4 Replies)
Discussion started by: ahfze
4 Replies

2. Shell Programming and Scripting

Insert newline when grep result is empty

Given a csv file with 40 columns with name, address, hometown etc. I use a bash command in 1 line which: 1. gets the address column and pipes that to 2. grep the first digit and everything that follows Command: awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv | grep -o "\d.*"... (7 Replies)
Discussion started by: JBVeenstra
7 Replies

3. Linux

Remove newline in middle of string

my file input is with tab as delimiter, and in every line, there would be a skip of line with an unexcepted newline breaker. I'd like to remove this \n and put the information in the same line. INPUT a1 b1b2 c1 c2 d1 a2 b3 c3 d4 OUTPUT a1 b1b2 c1c2 ... (9 Replies)
Discussion started by: kinkichin
9 Replies

4. Solaris

grep result in newline

Hi While trying to do a search on solaris, the grep results seems to be appearing on the same line instead of the new line. Wed Jan 18 14:45:48 weblogic@test:/abcd$ grep qainejb02 * qa_cluster_biz_view_tc_intl_servers_ports_2:qainejb02 7101 qa_cluster_servers_2:qainejb02... (2 Replies)
Discussion started by: ganga.dharan
2 Replies

5. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

6. Shell Programming and Scripting

Find zipcode with Grep with optional space in middle

Hello everyone. I am new to unix, nice to meet you all. I have a small problem with a question. I have to write a command that finds all the postal codes in a txt file. All postal codes will begin on its own line in the txt file. This is the formal of postal code: "A#A#A#" or "A#A #A#" A... (3 Replies)
Discussion started by: leonmerc
3 Replies

7. Shell Programming and Scripting

grep middle word between two patterns

Hi, I'm currently working on a shell script to automate a backup check on oracle database. My requirement is to grep the words between two delimiters and pass on to a variable.. for ex I have following values in my log file... (DB_NAME), (163.24 25), (16/02/10 23:40), (COMPLETED), I want... (5 Replies)
Discussion started by: senthil3d
5 Replies

8. Shell Programming and Scripting

Grep with wildcard in middle of word

How can grep G.*schema give me the result: ${Gacntg_dt}""'"' doesn't G.*schema say give me an unlimited number of characters between G and schema? :confused: (3 Replies)
Discussion started by: danmauer
3 Replies

9. Shell Programming and Scripting

grep the pattern followed by newline

Hi I have a problem using grep to find a pattern followed by newline. Here is my file xxxxxxxxxxxpattern patternxxxxxx pattern xxxpattern xxpatternx xxxxxxyyyyxxxx I want the result to be like this xxxxxxxxxxxpattern pattern xxxpattern Please help (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

grep and sed to find a pattern and add newline

Hello All, I have log file the result from a multithreaded process. So when a process finishes it will write to this log file as 123 rows merged. The issue is sometimes the processess finish at the same time or write to the file at the same time as 123 rows merged.145 rows merged. At... (5 Replies)
Discussion started by: ssikhar
5 Replies
Login or Register to Ask a Question