I have a filename with the format yyyymmdd.txt
. How can I output only yyyymmdd
without the .txt
extension?
Example
20220414.txt (before output)
20220414 (after the output)
I have a filename with the format yyyymmdd.txt
. How can I output only yyyymmdd
without the .txt
extension?
Example
20220414.txt (before output)
20220414 (after the output)
basename
has an option to remove a suffix:
basename -s .txt 20220414.txt
gives:
20220414
Or, if your filename is stored in a variable, bash can help:
a=20220414.txt
echo ${a%.*}
gives:
20220414
You can user awk
with flag -F
to specify the separator .
and then print the first part with $1
echo "20220414.txt" | awk -F "." ' {print $1}'
output
20220414
grep
doesn't manipulate anything, it shows what you have in a file. So, you can't modify that file using grep
, but you can modify what it shows, using the -o
switch, as you can see here:
Prompt> echo "20220414.txt" | grep -o "[0-9]*"
20220414
The [0-9]*
means "a list of integers, going from character '0'
to character '9'
.