0

I have a file name like below, and I want to print the file name before .tar. How I can do this?

Note: the part after .tar is fixed but the part before .tar is variable.

Example: abcd_ef_1.2.3.12+all.tar.gz.md5sum

lgeorget
  • 13,656
  • 2
  • 41
  • 63
  • Looks like a duplicate of http://unix.stackexchange.com/questions/153409/get-the-part-of-a-line-before-the-last-slash or http://unix.stackexchange.com/questions/304150/extract-everything-before-the-matching-string – lgeorget Sep 15 '16 at 15:12

3 Answers3

0

Using sed for example:

sed -E 's/(.*)\.tar.*/\1/' <<< "abcd_ef_1.2.3.12+all.tar.gz.md5sum"

prints abcd_ef_1.2.3.12+all.

lgeorget
  • 13,656
  • 2
  • 41
  • 63
0
$ basename -s .tar.gz.md5sum 'abcd_ef_1.2.3.12+all.tar.gz.md5sum'
abcd_ef_1.2.3.12+all

With Parameter Expansion

$ s='abcd_ef_1.2.3.12+all.tar.gz.md5sum'
$ echo "${s%.tar*}"
abcd_ef_1.2.3.12+all
Sundeep
  • 11,753
  • 2
  • 26
  • 57
0

combine find and sed maybe?

find . -maxdepth 1 -name "*.tar*" | sed 's/\(.*\)\(\.tar.*\)/\1/g'

explanation: the first part of sed

\(.*\)\(\.tar.*\)

find the tar, but provide 2 grouping. 1 before the .tar and 1 after the tar

the second part of sed

\1

take only the first grouping(before the .tar)

user2773013
  • 191
  • 1
  • 1
  • 3