Unix & Linux Asked on December 6, 2021
Text file(copyright) having a content like below.
gems/1.8/gems/fxri-0.3.6/fxri-0.3.6.tar.gz/fxri-0.3.6.tar/lib/Icon_Loader.rb
misc/common/groovy/groovy-src-1.7.0.tar.gz/groovy-1.7.0.tar/src/examples/swing/RegexCoach.groovy
……many more file path like this
I want to replace filename ended with tar.gz to empty space.
expected:
gems/1.8/gems/fxri-0.3.6/fxri-0.3.6.tar/lib/Icon_Loader.rb
misc/common/groovy/groovy-1.7.0.tar/src/examples/swing/RegexCoach.groovy
This did not work:
sed -i -e 's/*.tar.gz//g' copyright
Need help.
As a glob, *.tar.gz
means any string ending with .tar.gz
. Sed, however, does not use globs, it uses regular expressions and regular expressions have a different syntax. Try:
sed -i -e 's//[^/]*.tar.gz//g' copyright
In the above, the regex /
matches anything that starts with /
(because /
is being used as the sed
divider, we escape it as /
) followed by [^/]*
which matches zero or more of any character except /
, followed by .tar.gz
where the .
are escaped so that they match only periods. (In normal regex notation, .
matches any one character.)
There are many difference between globs (as used by the shell) and regular expressions (which are used by sed, grep, and other important tools). In a glob, .
means a period. In regex, .
is a wildcard meaning any single character. In a glob, *
means zero or more of any character. In a regex, *
means zero of more of the preceding thing.
We don't have to use /
as the divider in a sed substitute command. Other dividers are possible such as @
:
sed -i -e 's@/[^/]*.tar.gz@@g' copyright
Because in the above, we use @
as the dividor instead of /
, we have no need to escape the first /
.
Answered by John1024 on December 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP