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 8706609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:39:19+00:00 2026-06-13T03:39:19+00:00

I want to store a specific part of a matched result as a variable

  • 0

I want to store a specific part of a matched result as a variable to be used for replacement later. I would like to keep this in a one liner instead of finding the variable I need before hand.

when configuring apache, and use mod_rewrite, you can specificy specific parts of patterns to be used as variables,like this:

RewriteRule ^www.example.com/page/(.*)$ http://www.example.com/page.php?page=$1 [R=301,L]

the part of the pattern match that’s contained inside the parenthesis is stored as $1 for use later. So if the url was www.example.com/page/home, it would be replaced with www.example.com/page.php?page=home. So the “home” part of the match was saved in $1 because it was the part of the pattern inside the parenthesis.

I want something like this functionality with a sed command, I need to automatically replace many strings in a SQL dump file, to add drop table if exist commands before each create table, but I need to know the table name to do this, so if the dump file contains something like:

...
CREATE TABLE `orders`
...

I need to run something like:

cat dump.sql | sed "s/CREATE TABLE `(.*)`/DROP TABLE IF EXISTS $1\N CREATE TABLE `$1`/g"

to get the result of:

...
DROP TABLE IF EXISTS `orders`
CREATE TABLE `orders`
...

I’m using the mod_rewrite syntax in the sed command as a logical example of what I’m trying to do.

Any suggestions?

  • 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-06-13T03:39:20+00:00Added an answer on June 13, 2026 at 3:39 am
    sed '/CREATE TABLE \([^ ]*\)/ s//DROP TABLE IF EXISTS \1; &/'
    

    Find a CREATE TABLE statement and capture the table name. Replace it with ‘DROP TABLE IF EXISTS’ and the table name, plus a semi-colon to terminate the statement, and a copy of what was matched to preserve the CREATE TABLE statement.

    This is classic sed notation. Since you’re using bash, there’s a chance you’re using GNU sed and will need to add --posix to use that notation, or you’ll need to fettle the script to use GNU’s non-standard sed regexes. I’ve also not attempted to insert a newline into the output. You can do that with GNU sed if it is important enough to you.

    The key points are the parentheses (classically needing to be escaped with a backslash) are the capture mechanism, and backslash-number is the replacement mechanism.

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

Sidebar

Related Questions

edit I do not want to redirect pages, specific files etc. I would like
I want to store the password used for signing in a financial application that
Let's say I have table with column 'URL' whrere I store urls like this
I'm using Spring MVC and I want to store request specific values somewhere so
I want to store multiple mp3 files and search them by giving some part
I want to declare a variable which holds a class which implements a specific
I want a stored procedure to execute so that it updates a specific column
I am a new developer on Android and I want store user data in
I want to store an image in my SQLITE db. I checked out the
I want to store a user setting(language). so i can get the corresponding resources

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.