The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing new lines in all files of a directory containing old lines rooster005 Shell Programming and Scripting 1 03-25-2008 03:38 PM
How to delete first 5 lines and last five lines in all text files ragavendran31 Shell Programming and Scripting 10 02-21-2008 07:58 AM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 02:21 AM
need help appending lines/combining lines within a file... mr_manny Shell Programming and Scripting 2 01-06-2006 06:45 PM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 07:36 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2008
jjp jjp is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
X num of lines before and after

I have a slew of files for historical reference.

Each file has a unique line in the file, such as "Today's datetime is:"

Each file also contains a unique set of characters on line X, such as "DMX Info." This line number will be different each time a new file is generated. So, for one file, it might be line 100, whereas the next file might be line 120.

The important info I'd like is always two lines below line X above, but not unique enough to simply grep on by itself. It's called "High Value" and there are several lines like this in each file.

QUESTION:
How do I grep for "Today's datetime is" AND two lines below line X? I don't care if line X is printed. It's the line two lines below that I'm looking for.

THANKS!
  #2 (permalink)  
Old 09-15-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
not the cleanest solution but:

Code:
awk '/Today's datetime is/{print}/DMX Info/{print t}!/DMX Info/{t=s;s=$0}' file
should work
  #3 (permalink)  
Old 09-15-2008
jjp jjp is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
Thanks for that! Looks clean enough, but appears to be grabbing two lines before line X. I'm looking for two lines after line X. Any insight?

Thanks again!
  #4 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,322
Try this:
Code:
awk '/Today.s datetime is/;/DMX Info/{getline;print;getline;print;exit}'
Regards
  #5 (permalink)  
Old 09-15-2008
jjp jjp is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
That almost works, but it appears to only scan one file, whereas I'm trying to scan hundreds. Also, anyway to omit printing the first line after line X, and only print the second.

This is very helpful, so thanks guys (or gals).

Last edited by jjp; 09-15-2008 at 02:20 PM.. Reason: incorrect
  #6 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,322
This should omit the first line after x and scan all files in the current directory:

Code:
awk '/Today.s datetime is/;/DMX Info/{getline;getline;print}' *

Last edited by Franklin52; 09-15-2008 at 02:44 PM.. Reason: code adapted
  #7 (permalink)  
Old 09-15-2008
jjp jjp is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
There we go. That works, but it appears the 'exit' won't look in all files. For some reason it only scans one. If I omit the 'exit', it works perfectly.

Thanks. Shell scripting is not my strong point, but I need to get moving on it. Thanks again!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:27 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0