LinuxQuestions.org
Review your favorite Linux distribution.
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 04-24-2009, 01:44 AM   #1
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 248

Rep: Reputation: 31
sed two lines down


The following line gives me a nice list of telephone numbers:
sed -n 'N;s/.*Penang.*\n.*Tel: \([^,]*\).*/\1/p' < /home/dan/Desktop/0001

on the second line down, there is a fax number, starting with Fax: ; but i can't figure out how to add another line break to that command

the input looks like this, for example:

Penang, Plot 80 (A), Bayan Lepas Industrial Zone, Phase IV, MK.12, Lintang Bayan Lepas 6, 11900 Bayan Lepas, Penang
Tel: +60 (0)4 644 1795
Fax: +60 (0)4 645 6803
email: someone@somecompany.com

sed -n 'N;s/.*Penang.*\n.*Tel: \([^,]*\).*/\1/p' < /home/dan/Desktop/0001

the previous command outputs:
+60 (0)4 644 1795
(actually a list of about 40 phone numbers, all from different companies in penang)

but I'd like to get the fax number that is one line down, and the email address that is one line further.
 
Old 04-24-2009, 05:01 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,267

Rep: Reputation: 99
Lightbulb

Hi,

May be these examples can help you out.
Code:
   # insert a blank line ABOVE every line which matches "regex"
     sed '/regex/{x;p;x;}'
   
   # insert a blank line BELOW every line which matches "regex"
     sed '/regex/G'
   
   # insert a blank line ABOVE and BELOW every line which matches "regex"
     sed '/regex/{x;p;x;G;}'
 
Old 04-24-2009, 10:56 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I'm sure that sed could be used to do this, but I do think that awk is the tool to use.

awk 'BEGIN { FS="\n" ; RS="" ; OFS="\n" ; ORS="\n\n" } $1 ~ /Penang/ { print $2, $3, $4 }' secretlydead.infile

I don't know if you have any awk knowledge, so here's a breakdown of the command:

BEGIN { FS="\n" ; RS="" ; OFS="\n" ; ORS="\n\n" } -> This sets the required input and output separators. By default awk sees 1 line as 1 record, all the fields in a record are separated by spaces. Your infile has a different layout: all fields are on 1 line and a record is separated by a blank line.
FS="\n" ; RS="" sets the field separator to a newline (all fields are on a separate line) and the record separator to a blank.
OFS="\n" ; ORS="\n\n" sets the output separators (i'll let you figure out why by yourself ).

$1 ~ /Penang/ { print $2, $3, $4 } -> This looks for Penang in the first field ($1) and if it is found it will print fields 2, 3 and 4 (tel, fax and e-mail).

Here's a very basic script which accepts a searchstring as input and uses that on a specific file:
Code:
#!/bin/bash

if [[ -z $1 ]]
then
  echo ""
  echo "Nothing to search for, please provide search criterea."
  echo ""
  echo "  secretlydead.script token"
  echo ""
  exit 1
fi

INPUT="$1"

awk 'BEGIN { FS="\n" ; RS="" ; OFS="\n" ; ORS="\n\n" }  $1 ~ /'$INPUT'/ { print $2, $3, $4 }' secretlydead.infile
Hope this helps.
 
  


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
Comment lines 1-9, 11-19, 21-29 using Sed kushalkoolwal Programming 8 02-03-2009 04:02 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration