1

I have a string example.123.ytu.tar.gz

I want to have example.123.ytu.tar how can i get in shell scripting

I tried with

echo example.123.ytu.tar | cut -d "." -f3, But it is giving only tar

3 Answers 3

1

I would use basename for this.

$ basename --suffix ".gz" example.123.ytu.tar.gz
example.123.ytu.tar

You can find out more about it via man basename

Sign up to request clarification or add additional context in comments.

Comments

0
echo example.123.ytu.tar.gz | cut -d "." -f1-4
example.123.ytu.tar

The -f option takes any comma separated list of fields and/or field ranges (with '-'). Eg

echo example.123.ytu.tar.gz | cut -d "." -f1,2,3,4
example.123.ytu.tar

gives the same output.

2 Comments

Sorry, I was not clear in my question. The exact thing I want is to have a string till *.tar or before ".gz"
I have edited the answer. But if you look at the previous example you should see how to get the result you wanted.
0

You could simply use grep

echo example.123.ytu.tar | grep -Eo '.*tar'

output: example.123.ytu.tar

The -E option enables extended regular expression mode and the -o option makes grep print only the part of the word that matched the regex.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.