LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2010, 04:56 AM   #1
zeroquaranta
LQ Newbie
 
Registered: Feb 2010
Posts: 22

Rep: Reputation: 0
remove first and last character


Hi all,

which is the simplest way to have first and last character cropped out from a string?
Something simpler than
Code:
echo $STRING | cut -b 2- | rev |  cut -b 2- | rev
?
 
Old 02-24-2010, 06:59 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Normally, you should start new threads rather than jumping into exisiting ones. I've moved this to a new thread.
Code:
sed -e 's/^.//' -e 's/.$//'
(Not sure it's any better than yours.....)
 
Old 02-24-2010, 07:08 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
sed -r 's/^.(.*).$/\1/'
???
 
Old 02-24-2010, 09:20 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Parameter Expansion:
Code:
string="foobar"
echo "${string:1:$(( ${#string} - 2 ))}"
 
Old 02-24-2010, 09:28 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by tuxdev View Post
Parameter Expansion:
Code:
string="foobar"
echo "${string:1:$(( ${#string} - 2 ))}"
You don't need the $(()):

3.5.3 Shell Parameter Expansion
Quote:
${parameter:offset:length}
... length and offset are arithmetic expressions.
 
Old 02-24-2010, 10:14 AM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
ah, I missed that part of the manual
 
Old 02-25-2010, 02:10 AM   #7
zeroquaranta
LQ Newbie
 
Registered: Feb 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Sorry for not having opened a new thread.
Thank you all guys for the help.
And thank you for pointing that bash reference manual out. Looks very comprehensive.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
Remove a new line character kbmukesh Linux - Newbie 3 04-13-2009 12:41 AM
How do I remove everything after a certain character in text files? SentralOrigin Linux - General 3 01-14-2009 02:15 PM
How do I remove lines that have more than one of a certain character in them? SentralOrigin Linux - General 2 01-14-2009 01:40 PM
remove the 'd' character from the end of string powah Programming 6 11-08-2007 07:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration