LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-24-2007, 06:53 PM   #1
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Rep: Reputation: 16
shell script find a line and the next line (grep?)


Lets say I have the following text file

Code:
name
john123

phone:
123-213-4566

name
chris035

phone:
236-555-6546

name
bob56

phone:
333-666-9852
is there a way to find all the line that have "phone" and print them and the next line to make the out put look like this:
Code:
phone:
123-213-4566
phone:
236-555-6546
phone:
333-666-9852
I've looked into using grep awk & sed

I'm sure there is a way but I have yet to find it.
 
Old 07-24-2007, 07:00 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
grep -A1 phone $file


This only works with GNU grep. Normal grep doesn't have -A (after) option ( see also -B => before)
 
1 members found this post helpful.
Old 07-24-2007, 07:02 PM   #3
dxqcanada
Member
 
Registered: Sep 2006
Location: Canada
Distribution: Gentoo
Posts: 702

Rep: Reputation: 43
Awk

Awk can handle multiline records by specifying the field separator to be a newline and set the record separator to an empty string.
 
Old 07-24-2007, 07:13 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example using sed.
Code:
sed -n '/phone/,+1 p' file.txt
 
Old 07-24-2007, 08:25 PM   #5
metalx1000
Member
 
Registered: Jun 2006
Distribution: Debian
Posts: 112

Original Poster
Rep: Reputation: 16
All great answers.

Thanks!!!
 
Old 07-24-2007, 08:41 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/phone/{where=NR;print}NR==where+1 && where!=0 {print}' file
 
  


Reply



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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
Command line tool to find(1) or grep(1) faster? rsheridan6 Linux - Software 2 02-18-2006 03:50 PM
shell script that read each line separatly xpucto Programming 6 09-20-2005 08:06 AM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM
PHP: what's wrong with this line (shell_exec, find, xargs, grep)? J_Szucs Programming 3 11-19-2003 07:44 PM

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

All times are GMT -5. The time now is 03:08 PM.

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