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

  • SEARCH
  • Home
  • 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 9139599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:24:31+00:00 2026-06-17T09:24:31+00:00

I am new to shell scripts. I want to read a file line by

  • 0

I am new to shell scripts. I want to read a file line by line, which contains arguments and if the arguments contains any spaces in it, I want to replace it by enclosing with quotes.

For example if the file (test.dat) contains:

-DtestArgument1=/path/to a/text file
-DtestArgument2=/path/to a/text file

After parsing the above file, shell script should prepare the string with following:

-DtestArgument1="/path/to a/text file" -DtestArgument2="/path/to a/text file"

Here is my shell script:

while read ARGUMENT; do
    ARGUMENT=`echo ${ARGUMENT} | tr "\n" " "`
        if [[ "${ARGUMENT}" =~ " " ]]; then
            ARGUMENT=`echo $ARGUMENT | sed 's/\^(-D.*\)=(.*)/\1=\"\2\"/g'`
            NEW_ARGUMENT="${NEW_ARGUMENT} ${ARGUMENT}"
        else
            echo "doesn't contains spaces"
            NEW_ARGUMENT="${NEW_ARGUMENT} ${ARGUMENT}"
        fi
    done < test.dat

But it’s throwing the following error:

sed: -e expression #1, char 28: Unmatched ) or \)

The code should be compatible with all shells.

  • 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-17T09:24:32+00:00Added an answer on June 17, 2026 at 9:24 am

    The sed parentheses should be escaped:

    ARGUMENT=`echo $ARGUMENT | sed "s/\^\(-D.*\)=\(.*\)/\1=\"\2\"/g"`
    

    One place you did, in 3 places you forgot… BTW, I generally use " quotation.

    If you prefer '-style, do like this:

    ARGUMENT=`echo $ARGUMENT | sed 's/\^(-D.*)=(.*)/\1="\2"/g'`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to shell scripts, and I have centos running. I want to write
So, I'm new to shell scripts and I'm really doing this as a learning
I am new at shell scripting and I am trying to read data from
I am new with shell scripting. I am having a file containing some records
I have a Shell Scripts that read the Input #!/bin/bash echo Type the year
Consider a ASCII text file (lets say it contains code of a non-shell scripting
I am pretty new to shell scripting and I have to add a flag
I am new to shell scripting. so kindly bear with me if my doubt
I'm new to shell,I just learned that use (command) will create a new subshell
Forgive my ignorance, but I am somewhat new to shell scripting and the use

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.