finding the last substring...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding the last substring...
# 1  
Old 11-03-2006
finding the last substring...

hii,
i want to know the shell command for finding the last occurance of a substring in string..
i can use grep command or sed to find out the occurance of a substring in a string but how do i find out the last occurance.shud i use grep amd and cut the string everytime and store it in a new variable until i get null for the grep..?

Thanks!
# 2  
Old 11-03-2006
One way :
Code:
STRING="aaa;bbb;ccc"
SUBSTRING=';'
last_position=$(echo "$STRING" | awk -v str=$SUBSTRING '
   {
      while (match(substr($0, pos+1), str)) pos += RSTART;
      print pos;
      exit;
   } ')


Jean-Pierre.
# 3  
Old 11-03-2006
This code doesnot work...itz giving me the following error..
STRING="aaa;bbb;ccc"
SUBSTRING=';'
last_position=$(echo "$STRING" | awk -v str=$SUBSTRING '
{
while (match(substr($0, pos+1), str)) pos += RSTART;
print pos;
exit;
} ')

Errors:
awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 11-03-2006
Try with nawk instead of awk.


Jean-Pierre.
# 5  
Old 11-03-2006
even this doesnot work...i had previously written similarly..it doesnot work...
# 6  
Old 11-04-2006
Does thie help ?
Code:
STRING="aaa;bbb;ccc"
SUBSTRING=';'
echo ${STRING##*$SUBSTRING}

# 7  
Old 11-04-2006
This is what you are looking for

use this command

Code:
awk '{print $NF}' file_name

Thanks
srikanth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding a word through substring in a file

I have a text file that has some data like: PADHOGOA1 IOP055_VINREG5_1 ( .IO(VINREG5_1), .MONI(), .MON_D(px_IOP055_VINREG5_1_MON_D), .R0T(px_IOP054_VINREG5_0_R0T), .IO1() ); PADV30MA0 IOP056_VOUT3_IN ( .IO(VOUT3_IN), .V30M(px_IOP056_VOUT3_IN_V30M)); PADV30MA0 IOP057_VOUT3_OUT (... (2 Replies)
Discussion started by: utkarshkhanna44
2 Replies

2. UNIX for Dummies Questions & Answers

substring

hi , I have complete path Sample: /pkgs/Incoming/Completed/abc123_xyz.zip /pkgs/Incoming/Completed/12_abcxyz.zip /pkgs/Incoming/Completed/qwabcxyz.zip i just need , so that i can copy these files to different directory, abc123_xyz.zip 12_abcxyz.zip qwabcxyz.zip (5 Replies)
Discussion started by: Naveen_5960
5 Replies

3. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

4. Shell Programming and Scripting

Finding duplicates from positioned substring across lines

I have million's of records each containing exactly 50 characters and have to check the uniqueness of 4 character substring of 50 character (postion known prior) and report if any duplicates are found. Eg. data... AAAA00000000000000XXXX0000 0000000000... upto50 chars... (2 Replies)
Discussion started by: gapprasath
2 Replies

5. Shell Programming and Scripting

Finding longest common substring among filenames

I will be performing a task on several directories, each containing a large number of files (2500+) that follow a regular naming convention: YYYY_MM_DD_XX.foo_bar.A.B.some_different_stuff.EXT What I would like to do is automatically discover the part of the filenames that are common to all... (1 Reply)
Discussion started by: cmcnorgan
1 Replies

6. Shell Programming and Scripting

help for shell script of finding shortest substring from given string by user

please give me proper solution for finding a shortest substring from given string if string itself and first char and last char of that substr are also given by user if S="dpoaoqooroo" and FC="o" and LC="o",then shortest substr is "oo" and rest of the string is "dpoaoqroo" i have code but it is... (1 Reply)
Discussion started by: pankajd
1 Replies

7. Shell Programming and Scripting

How do I Substring ??

Hello everyone. I'm writing a script in UNIX. The purpose is to get the second character from a variable that stores the system year. This is the code: unix_year_yy=`date "+%g"` This will return "07" in variable unix_year_yy. How can I get the second character (7)?? (6 Replies)
Discussion started by: Rigger
6 Replies

8. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

9. Shell Programming and Scripting

how to get substring

i have a strings abc-def.csv ghi-jkl.csv i want to make it as abc-*-def.xyz ghi-*-jkl.xyz How to do it?. (5 Replies)
Discussion started by: senthilk615
5 Replies

10. Shell Programming and Scripting

Getting a substring

This is probably pretty simple butI'm not sure how to best go about it. If I have FILE="myBigLongFileName_1.xls" FILE_PREFIX=`echo $FILE| cut -d"." -f1` # that gives "myBigLongFileName_1" All i want to do now is chop the "_1" from the end of $FILE_PREFIX Any ideas anyone? (3 Replies)
Discussion started by: djkane
3 Replies
Login or Register to Ask a Question