Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 04-24-2009, 01:44 AM   #1
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

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

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
Senior Member
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107


May be these examples can help you out.
   # 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
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395

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:

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


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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 AM.

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