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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:50:49+00:00 2026-06-11T11:50:49+00:00

Is fairly easy to strip the first and last character from a string using

  • 0

Is fairly easy to strip the first and last character from a string using awk/sed?

Say I have this string
( 1 2 3 4 5 6 7 )

I would like to strip parentheses from it.

How should I do this?

  • 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-11T11:50:50+00:00Added an answer on June 11, 2026 at 11:50 am

    sed way

    $ echo '( 1 2 3 4 5 6 7 )' | sed 's/^.\(.*\).$/\1/'
     1 2 3 4 5 6 7 
    

    awk way

    $ echo '( 1 2 3 4 5 6 7 )' | awk '{print substr($0, 2, length($0) - 2)}'
     1 2 3 4 5 6 7 
    

    POSIX sh way

    $ var='( 1 2 3 4 5 6 7 )'; var="${var#?}"; var="${var%?}"; echo "$var"
     1 2 3 4 5 6 7 
    

    bash way

    $ var='( 1 2 3 4 5 6 7 )'; echo "${var:1: -1}"
     1 2 3 4 5 6 7 
    

    If you use bash then use the bash way.

    If not, prefer the posix-sh way. It is faster than loading sed or awk.

    Other than that, you may also be doing other text processing, that you can combine with this, so depending on the rest of the script you may benefit using sed or awk in the end.


    why doesn’t this work? sed '..' s_res.temp > s_res.temp ?

    This does not work, as the redirection > will truncate the file before it is read.
    To solve this you have some choices:

    1. what you really want to do is edit the file. sed is a stream editor not a file editor.
      ed though, is a file editor (the standard one too!). So, use ed:

       $ printf '%s\n' "%s/^.\(.*\).$/\1/" "." "wq" | ed s_res.temp
      
    2. use a temporary file, and then mv it to replace the old one.

       $ sed 's/^.\(.*\).$/\1/' s_res.temp > s_res.temp.temp
       $ mv  s_res.temp.temp  s_res.temp
      
    3. use -i option of sed. This only works with GNU-sed, as -i is not POSIX and GNU-only:

      $ sed -i 's/^.\(.*\).$/\1/' s_res.temp
      
    4. abuse the shell (not recommended really):

       $ (rm test; sed 's/XXX/printf/' > test) < test
      

    On Mac OS X (latest version 10.12 – Sierra) bash is stuck to version 3.2.57 which is quite old. One can always install bash using brew and get version 4.x which includes the substitutions needed for the above to work.

    There is a collection of bash versions and respective changes, compiled on the bash-hackers wiki

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

Sidebar

Related Questions

This seems fairly easy but I can't focus. Let's say I have got 2
I thought this would be fairly easy, but I'm totally baffled. I want one
I'm a beginner at SQL and have this fairly easy conditional problem: Every installation
So I just set this up: http://www.phpeasystep.com/phptu/6.html It was fairly easy, now I have
I have replicated the classic helicopter game using libgdx. It was fairly easy. Now
This might be fairly easy to answer. I have two scripts (i.e. ScriptA.app and
This should be fairly easy, but I'm getting a headache from trying to figure
I'm sure there's a fairly easy way to do this. I have an the
I can define church numerals fairly easy using scheme: > (define f (lambda (x)
I'm fairly new to ASP MVC so go easy :). I have a form

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.