get the last 2 char from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get the last 2 char from a variable
# 1  
Old 10-12-2005
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  
Old 10-12-2005
Code:
echo $VAR1 | sed 's/.*\(..\)\..*/\1/'

NT

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

XP

# 3  
Old 10-12-2005
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  
Old 10-12-2005
Code:
echo $VAR1 | ruby -ne 'puts $_[/(..)\./,1]'

# 5  
Old 10-12-2005
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  
Old 10-13-2005
Perhaps just use expr instead of echo|sed...
Code:
expr $VAR1 : ".*\(..\)\..*"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

3. UNIX for Dummies Questions & Answers

Remove newline char from variable

I have a file ABC.DAT with 2 columns avaialble Data format : XYZ!$#$!120 XXZ!$#$!1000 YYZ!$#$!104 While running the following code : FILE_COUNTER=1; RECORD_CN_FILE_COUNT=$((`wc -l ABC.DAT| cut -f1 -d' '`)); while do FILE_NAME=`cat ABC.DAT.DAT| head -$FILE_COUNTER |tail -1 | awk -F... (1 Reply)
Discussion started by: Nikhil Gautam
1 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. Shell Programming and Scripting

A variable is including the Carriage Return char...

Hi all, I'm reading a file with this layout: First_Col Second_Col The Second_Col has values as 1000, -1, 10, 43... While reading the file I'm getting the second column value with awk command, but it is including the CR control char. do item_saved=`echo $b | awk '{print... (4 Replies)
Discussion started by: mrreds
4 Replies

6. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

7. Shell Programming and Scripting

Splitting a variable based on newline char

Heeloo all, A weird problem perhaps. May god save others from this problem. I want to print each line from a variable.. the example below should make it clear. smvar="Hello World1 Hello world 2 forgot there I guess" for eachline in $smvar echo $eachline end Whats for... (3 Replies)
Discussion started by: pavanlimo
3 Replies

8. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

9. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

10. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies
Login or Register to Ask a Question