LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-08-2011, 05:40 AM   #1
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Rep: Reputation: 21
sed block search with variables


Hi Folks,

1) extracting a block of text from file1 to file2 works fine this hardcoded way:

sed -n '/DW/,/;/p' file1.tmp > file2.tmp

2) Now I need it more flexible using a shell variable:

p=DW

sed -n '/$p/,/;/p' file1.tmp > file2.tmp

But the file2.tmp stays empty. What is wrong ? Thank you all.
 
Old 03-08-2011, 05:55 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
The single quotes prevent shell substitution and the string $p is passed literally to sed and interpreted with its own rules. To let the shell make the substitution and expand the value of the variable, use double quotes.
 
Old 03-09-2011, 02:51 AM   #3
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Original Poster
Rep: Reputation: 21
double quotes

Colucix,

thanks for your reply. I tried several double quotes but no joy. How should that look like in my case ?

cheers from Germany :-)
 
Old 03-09-2011, 04:23 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
This works on my system:
Code:
sed -n "/$p/,/;/p" file
using GNU sed version 4.1.5.
 
1 members found this post helpful.
Old 03-10-2011, 08:37 AM   #5
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Original Poster
Rep: Reputation: 21
Thank you ! That is the solution. Have a great day. :-)
 
  


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
SED with variables RocketJumper Programming 15 02-06-2011 11:01 AM
Sed and variables oden Programming 9 07-29-2007 05:36 PM
sed and bash variables cashton2k Programming 1 03-23-2007 05:43 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
passing variables to sed jjfate Programming 8 07-31-2003 05:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration