LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 04: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, 04:55 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
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, 01: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, 03:23 AM   #4
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
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, 07: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



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

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

All times are GMT -5. The time now is 08:40 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