Reputation: 187
can some one explain me how to cut some string with using linux features like sed. For example I have sting
THIS-some-string-zzz-55.xml
how to cut ".xml" ?
result should be like:
THIS-some-string-zzz-55
Thanks!
Upvotes: 2
Views: 2567
Reputation: 58351
Does Bash count as a Linux feature?
If so, this might work for you:
# string="THIS-some-string-zzz-55.xml"
# echo ${string%.xml}
THIS-some-string-zzz-55
or in this case:
# echo ${string%.*}
THIS-some-string-zzz-55
See here for explanation.
Upvotes: 0
Reputation: 21972
Grep
time
$> echo THIS-some-string-zzz-55.xml | grep -o -P "(.*)(?=\.xml)"
THIS-some-string-zzz-55
Grep have a magic -P flag
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression. This is highly
experimental and grep -P may warn of unimplemented features.
Btw, here is a useful table, where you can find positive lookahead (?=*)
, that I'm using here.
Upvotes: 1
Reputation: 3351
A few ways:
basename thisfile.xml .xml
basename is it's own executable, so you can call it from a shell script or exec it from C or a scripting language.
If your shell is bash:
FILE=filename.xml
echo “filename: ${file%.*}”
echo “extension: ${file##*.}”
..and finally with sed
echo "filename.xml" | sed 's/\.xml$//'
That '$' in the regular expressin in sed will make the .xml match only at the end of the string.
Upvotes: 5
Reputation: 1541
This should work:
echo "THIS-some-string-zzz-55.xml" | sed 's/\.xml$//'
Upvotes: 0