URG!! Last position of a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting URG!! Last position of a character
# 1  
Old 06-23-2008
Error URG!! Last position of a character

Hi.
If I have files with names like abcd.20080625.1234.abc.XYZ abcd.20080625.1234.abc.XYZW

how can I get the XYZ or XYZW part? is there something like an Index() function but in reverse order?
I was thinking that if I can get the position of the last dot (.) I can use substr, but i dont know how to get the last position of a character.

Thanks
# 2  
Old 06-23-2008
Shell solution.
Code:
a=abcd.20080625.1234.abc.XYZ
echo ${a#.*}

# 3  
Old 06-23-2008
Hammer & Screwdriver Will it always be after the 4th period "."?

Code:
>echo abcd.20080625.1234.abc.XYZ | cut -d"." -f5
XYZ
> echo abcd.20080625.1234.abc.XYZW | cut -d"." -f5
XYZW

# 4  
Old 06-23-2008
Quote:
Originally Posted by danmero
Shell solution.
Code:
a=abcd.20080625.1234.abc.XYZ
echo ${a#.*}


Tested shell solution:
Code:
  $ a=abcd.20080625.1234.abc.XYZ
$ echo ${a##*.}
XYZ
$

Smilie
# 5  
Old 06-23-2008
mmm no, the filename could have more than 4 periods. what I know for sure is that it will be after the last period.

Thanks.
# 6  
Old 06-23-2008
Quote:
Originally Posted by Perderabo
Tested shell solution:
Code:
  $ a=abcd.20080625.1234.abc.XYZ
$ echo ${a##*.}
XYZ
$

Smilie
Thanks !!

Does this works to find the x-ocurrence of a character? If i wanted the 3rd or the 6th position of the period?
# 7  
Old 06-23-2008
Try this:
Code:
$ cat looper
#! /usr/bin/ksh

a=abcd.20080625.1234.abc.XYZ

echo input = $a
while [[ -n $a ]] ; do
       if [[ $a = *.* ]] ; then
               piece=${a%.${a#*.}}
               a=${a#*.}
       else
               piece=$a
               a=""
       fi
       echo $piece
done
exit 0
$ ./looper
input = abcd.20080625.1234.abc.XYZ
abcd
20080625
1234
abc
XYZ
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Position of character in word

How can I represent the position of 1 (considering only the 1s after the colon) in the word from field5 and above; counting from right to left. Input: TT-124 06-03-14 08-02-10 FAS CAT1:10 TT-125-1 05-03-14 10-06-08 CAS CAT2:1010 FAT1:10000 TT-125-3 07-03-14 11-02-06 FAS FAT1:1101... (6 Replies)
Discussion started by: aydj
6 Replies

3. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

4. Shell Programming and Scripting

Get character position within variable

Hi all let's say i have a file named 1234_v2_abcd i need to find the position of the character located after _v, in above case this would be character number 2 the count of the characters before _v can change, but i always have a number after _v can anybody help :) (4 Replies)
Discussion started by: gigagigosu
4 Replies

5. Shell Programming and Scripting

position of character in a line

i want to find the position of a character in a line , the first position, last, 5th occurence position , i ve tried grep -n , and expr index but they dont fit the bill. Please let me know if there is any other alternative (2 Replies)
Discussion started by: phpsnook
2 Replies

6. Shell Programming and Scripting

Extract a character specifying position

hi , i am having a file Full_ARTMAS_20110510152425.xml in my local directory. i wanted to extract the character at the 35143546 th position at line 1 of this file.Can any body help me how to do it??? regards Anjali (2 Replies)
Discussion started by: angel12345
2 Replies

7. Shell Programming and Scripting

Identify the position of character

Hi, Can some one guide me to identify the position of a character using index in UNIX. I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space). I am able to... (15 Replies)
Discussion started by: suneel.mekala
15 Replies

8. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

9. UNIX for Dummies Questions & Answers

How to split a value according to character position

Hello all, I have a script which picks up a series of large numbers, each of which are actually amalgamations of a series of other numbers. Unfortunately there are no separator characters so I can't use awk -F. I am looking for a way of splitting them into variables according to their... (4 Replies)
Discussion started by: michaeltravisuk
4 Replies

10. UNIX for Dummies Questions & Answers

Character position

Hi , I am required to view the fixed postion file very often . I am looking for the utility like this if the file has a one or multile line abcdefghijklmnopqr Utility should make my file look like this 12345678910111213141516-------------------------- abcdefghijk l m n o p q r ... (4 Replies)
Discussion started by: akrathi
4 Replies
Login or Register to Ask a Question