Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:28:30+00:00 2026-05-31T06:28:30+00:00

I will show an example to explain my problem. VARIABLE=’some string’ I want to

  • 0

I will show an example to explain my problem.

VARIABLE='some string'

I want to search the string in file like:

sed -n "/$VARIABLE,test/p" aFile

I know it’s wrong, because the variable identifier “$” is conflict with perl pattern $ which means the end of the line.

So my question is: is there any way to use variable in sed patter search?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:28:32+00:00Added an answer on May 31, 2026 at 6:28 am

    It looks like you’re trying to use the sed range notation, i.e. /start/,/end/?. Is that correct?

    If so all you need to do is add the additional ‘/’ chars that are missing, i.e.

    sed -n "/$VARIABLE/,/test/p" aFile
    

    A range can be composed of line numbers, strings/regexs, and/or relative line numbers (with no negative look back).

     sed -n "1,20p" aFile
     # prints lines 1-20 
    
     sed -n '/start/,/end/p' aFile
     # prints any text (include current line)
     # with start until it finds the word end
    
     sed -n '/start/,+2p' aFile
     # prints line matching start and 2 more lines after it
    

    I am using strings in a /regex/ pattern to simplfy the explanation.
    You can do real rex-ex’s like /^[A-Za-z_][A-Za-z0-9_]*/ etc. as you learn about them.

    Also per your comment about ‘$’, sed will also use ‘$’ as an indicator of ‘end-of-line’ IF the character is visible in the reg-ex that is being evaluated. Also note how some of my examples use dbl-quotes OR single-quotes. Single quotes mean that your expression sed -n '/$VARIABLE/,/test/p' aFile would match the literal chars AND because the $ is not at the end of a reg-ex, it will be used as a regular character. The ‘$’ only applies as end-of-line, when it is at the end of a reg-ex (part); for example you could do /start$|start2$/, and both of those would signify end-of-line.

    As others have pointed out, your use of

    sed -n "/$VARIABLE/,/test/p" aFile
    

    is being converted via shell variable explasion to

    sed -n "/some text/,/test/p" aFile
    

    SO if you wanted to ensure your text was anchored at the end-of-line, you could write

    sed -n "/$VARIABLE$/,/test/p" aFile 
    

    which expands to

    sed -n "/some text$/,/test/p" aFile
    

    I hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will show the problem with a example. There is some text in the
How to do it. For example the calendar for march will also show days
I´m working on a project that basically will show some data collected from hardware
I want to write a page that will show all the records from a
Have a gridview control which will show X amount of rows. I want to
I will try and explain exactly what I want to achieve first. Imagine two
Hard to explain, and let me show an example. If username foo already exists
I'll explain my problem with an example. I have an AnimalService, allowing me to
I think this will be easier to show you than explain, but basically I
from this code will show in original color how can i change it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.