Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a string example.123.ytu.tar.gz
example.123.ytu.tar.gz
I want to have example.123.ytu.tar how can i get in shell scripting
example.123.ytu.tar
I tried with
echo example.123.ytu.tar | cut -d "." -f3, But it is giving only tar
echo example.123.ytu.tar | cut -d "." -f3
tar
I would use basename for this.
basename
$ basename --suffix ".gz" example.123.ytu.tar.gz example.123.ytu.tar
You can find out more about it via man basename
man basename
Add a comment
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
-f
echo example.123.ytu.tar.gz | cut -d "." -f1,2,3,4 example.123.ytu.tar
gives the same output.
You could simply use grep
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.
-E
-o
Required, but never shown
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.
Explore related questions
See similar questions with these tags.