If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > command which will print only last character??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-01-07, 21:35
rfourn rfourn is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
command which will print only last character??

i'm trying to find a command that will print only the last character
stored in a shell variable.

i know how to delete the last character:
sed 's/.$//'

but when i try to use the a similar command with the reverse outcome:
sed 's/^[.$]//'

it does not work.
any suggestions are appreciated!
Reply With Quote
  #2 (permalink)  
Old 06-02-07, 04:20
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Code:
sed 's/^.*\(.\)$/\1/'
This regular expression uses a substring, between '\(' and '\)', to denote the last single character of the line in the search string. The replace string refers to the (first) substring with '\1' and so replaces the initial string with only the last character.
Note that
Code:
's/^[.$]//'
means a line with only one or none characters because the '^' used in this way means 'beginning of line'. To reverse a charactermatch use
Code:
's/[^.$]//'
which in this example doesn't do anything because it is searching for something thats not a character and not an 'end of line'.

Regards

Last edited by Tyveleyn; 06-02-07 at 04:29.
Reply With Quote
  #3 (permalink)  
Old 06-02-07, 08:30
rfourn rfourn is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
thank you very much...
Reply With Quote
  #4 (permalink)  
Old 06-02-07, 14:00
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
Quote:
Originally Posted by rfourn
i'm trying to find a command that will print only the last character
stored in a shell variable
[...]
Code:
$ v="abc"
$ echo "${v#${v%?}}"
c
Reply With Quote
  #5 (permalink)  
Old 06-05-07, 05:57
rfourn rfourn is offline
Registered User
 
Join Date: Jun 2007
Posts: 11
based on this answer, how would you go about printing the
FIRST character??
Reply With Quote
  #6 (permalink)  
Old 06-05-07, 06:01
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
Code:
$ v="abc"
$ echo "${v%${v#?}}"
a
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On