The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace any char with newline char. mightysam Shell Programming and Scripting 5 09-18-2008 08:15 PM
last char from a string broli Shell Programming and Scripting 6 12-07-2007 08:02 PM
char c = 882 useless79 High Level Programming 1 07-30-2007 05:16 AM
char *p and char p[]. arunviswanath High Level Programming 4 07-20-2006 02:11 AM
\n char in C C|[anti-trust] High Level Programming 1 05-05-2005 06:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-12-2005
tads98 tads98 is offline
Registered User
  
 

Join Date: May 2005
Posts: 40
get the last 2 char from a variable

i have 2 variables
VAR1=12345_SNT.in1
VAR2=123456_LXP.in2

how can i get the last 2 characters before the .
for VAR1, i only want to get the NT
for VAR2, i only want to get the XP

the length of the variable varies, so does its extension. the only thing that is consistent is the "."

thanks!
  #2 (permalink)  
Old 10-12-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Code:
echo $VAR1 | sed 's/.*\(..\)\..*/\1/'

NT

echo $VAR2 | sed 's/.*\(..\)\..*/\1/'

XP
  #3 (permalink)  
Old 10-12-2005
tads98 tads98 is offline
Registered User
  
 

Join Date: May 2005
Posts: 40
Quote:
Originally Posted by tmarikle
Code:
echo $VAR1 | sed 's/.*\(..\)\..*/\1/'

NT

echo $VAR2 | sed 's/.*\(..\)\..*/\1/'

XP
thanks! that's great! i tested it and it worked!

just to understant things... what does this mean? sed 's/.*\(..\)\..*/\1/'
  #4 (permalink)  
Old 10-12-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
echo $VAR1 | ruby -ne 'puts $_[/(..)\./,1]'
  #5 (permalink)  
Old 10-12-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Code:
sed = stream editor

general form = s/search expression/replace expression/

search expression = 
   .*     matches any string of characters
   \(..\) matches exactly two characters and preserves the matching values in a place holder
   \.     matches exactly one period; your constant
   .*     matches the remainder of the string

replace expression =
  \1 only inserts the entire string that was contained in \( and \)
  #6 (permalink)  
Old 10-12-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Perhaps just use expr instead of echo|sed...
Code:
expr $VAR1 : ".*\(..\)\..*"
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0