Extracting substring

Hi,

I have string in variable like '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010'
and i want to conevrt this string into only "DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010" (i.e file name).

Is there any command to extracting string in some part ?(rather than whole path)?

In my code ,i have done some logic on this file and i moved to in_arc folder after that i wann to compress using command

compress ../in_arc/$file

it's coming error as ../in_arc//u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010: No such file or directory

as file variable stores whole path.

Could you please advise me on this ?

basename '/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010'
DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010
file='/u/dolfin/in/DOLFIN.PRL_100.OIB.TLU.001.D20110520.T040010' 

file=${file##*/}

echo $file

Thank you Franklin it works.

Hi Franklin,

I am curious to know what does this '##*' mean in this regular expression?

Thanks!

This isn't a regex, it's a shell (kshell or bash) variable substitution syntax. The ##pattern and #pattern match the pattern at the beginning of the value contained by the variable and deletes it before the value is used. (The contents of the variable are unchanged)

The difference between the two is that ## matches the longest pattern and # matches the shortest. For example, if you had a filename /home/scooter/lib/cal.rb in variable fname and want the basename, coding ${fname##*/} matches all charcters (*) from the front of the string up to the right most slant. The result would be cal.rb

If you had coded [${fname#*/} then all characters up to the first slant would be removed and the result would have been home/scooter/lib/cal.rb .

You will also note that the "pattern" isn't a regular expression pattern, but a file globbing pattern and thus its ##*/ rather than ##.*/ . This can be confusing.

The advantage to using these, as opposed to $(basename $fname) , is that it is much more efficient to let the shell do the string manipulation than to invoke a separate process just to chop a string up.

Have a look at the Kshell man page as there are a lot of variable substitution tricks that can be used.
man/man1/ksh.html man page