0

I'd like to parse different kinds of Java archive with the sed command line tool.

Archives can have the followin extensions:
.jar, .war, .ear, .esb

What I'd like to get is the name without the extension, e.g. for Foobar.jar I'd like to get Foobar.

This seems fairly simple, but I cannot come up with a solution that works and is also robust.

I tried something along the lines of sed s/\.+(jar|war|ear|esb)$//, but could not make it work.

fgysin
  • 11,329
  • 13
  • 61
  • 94
  • possible duplicate of [Extract filename and extension in bash](http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash) – devnull Jan 22 '14 at 10:10

4 Answers4

1

Using sed:

s='Foobar.jar'
sed -r 's/\.(jar|war|ear|esb)$//' <<< "$s"
Foobar

OR better do it in BASH itself:

echo "${s/.[jwe]ar/}"
Foobar
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The last one is quite elegant, but doesn't match `esb`. Also, you can use the `${parameter%word}` syntax to only strip the ending of a name. – Lev Levitsky Jan 22 '14 at 10:34
1

You need to escape the | and the () and also add ' if you do not add option like -r or -E

echo "test.jar" | sed 's/\.\(jar\|war\|ear\|esb\)$//'
test

* is also not needed, sine you normal have only one .

Jotne
  • 40,548
  • 12
  • 51
  • 55
1

You were nearly there:

sed -E 's/\.+(jar|war|ear|esb)$//' file

Just needed to add the -E flag to sed to interpret the expression. And of course, respect the sed 's/something/new/' syntax.

Test

$ cat a
aaa.jar
bb.war
hello.ear
buuu.esb
hello.txt

$ sed -E 's/\.+(jar|war|ear|esb)$//' a
aaa
bb
hello
buuu
hello.txt
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

On traditionnal UNIX (tested with AIX/KSH)

File='Foobar.jar'
echo ${File%.*}

from a list having only your kind of file

YourList | sed 's/\....$//'

form a list of all kind of file

YouList | sed -n 's/\.[jew]ar$/p
t
s/\.esb$//p'
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43