Harish
Harish

Reputation: 3483

shell script to get the file name from a string

I need to get the basnename of the file (esrt-deghasdf-keystore) before .jks. I want to do it using shellscript. Is it possible?

abcdefgh 7369 4825 0 00:12:26 pts/10 0:37 java -Djavax.net.ssl.keyStore=/abc3/esrt/der/fer-def2/esrt-deghasdf-keystore.jks

Upvotes: 1

Views: 1220

Answers (3)

ghostdog74
ghostdog74

Reputation: 343067

no need external tools. ksh can do the job

$ var="abcdefgh 7369 4825 0 00:12:26 pts/10 0:37 java -Djavax.net.ssl.keyStore=/abc3/esrt/der/fer-def2/esrt-deghasdf-keystore.jks"

$ echo ${var##*/}
esrt-deghasdf-keystore.jks

$ var=${var##*/}
$ echo ${var%.*}

Upvotes: 2

Aryabhatta
Aryabhatta

Reputation:

Use cut piped to sed. (some thing like cut -f 7 | sed blah)

Sorry I don't remember exactly how to use both.

See the manpages: cut and sed

Upvotes: 1

Alok Singhal
Alok Singhal

Reputation: 96241

It depends upon the format of the line. If you lines are all going to end in /path/to/file.ext format, you can do:

echo $line | sed -e 's@.*/@@g' -e 's@\.[^.]*$@@g'

but really, it depends upon how exactly your lines are formatted and what you want out of it.

Upvotes: 1

Related Questions