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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
sed way
awk way
POSIX sh way
bash way
If you use
bashthen use the bash way.If not, prefer the posix-sh way. It is faster than loading
sedorawk.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
sedorawkin the end.This does not work, as the redirection
>will truncate the file before it is read.To solve this you have some choices:
what you really want to do is edit the file.
sedis a stream editor not a file editor.edthough, is a file editor (the standard one too!). So, useed:use a temporary file, and then
mvit to replace the old one.use
-ioption ofsed. This only works with GNU-sed, as-iis not POSIX and GNU-only:abuse the shell (not recommended really):
On Mac OS X (latest version 10.12 – Sierra)
bashis stuck to version3.2.57which is quite old. One can always installbashusingbrewand get version4.xwhich 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