LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2008, 09:30 AM   #1
eentonig
LQ Newbie
 
Registered: Jan 2008
Posts: 26

Rep: Reputation: 15
[SOLVED] find linenumber for pattern and work on the next line (Help Please)


Hi,

I know I'm missing something basic here.

I need to process a file with info I gathered from our routers and put those into a database. So far, so good.

But now I need to find a line and get info from the next line

Quote:
### INVENTORY ###
NAME: "chassis", DESCR: "1841 chassis"
PID: CISCO1841 , VID: V05 , SN: FCZ1143R27B
I know the "chassis" will always be above the line of interest. So I need to find which linenumber contains "chassis", and then I need to unleash the power of awk on the next line ("awk -F: `{ print $2}`").

Can someone show me a good oneliner to achieve this?

Thanks

Last edited by eentonig; 07-24-2008 at 12:01 AM. Reason: question answered.
 
Old 07-23-2008, 09:44 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
awk '/chassis/{print NR+1}' file
or
Code:
awk '$2~"chassis"{print NR+1}' file
The commands above retrieve the number of the line immediately below that containing chassis. If you want to do all the processing using awk you can use getline to read and process the line below the matching pattern, for example:
Code:
awk '/chassis/{getline; other commands here}'
 
Old 07-24-2008, 12:00 AM   #3
eentonig
LQ Newbie
 
Registered: Jan 2008
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks!!

I knew it was something obviously simple.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration