1

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)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
prg
  • 15
  • 3

3 Answers3

2

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
mouviciel
  • 66,855
  • 13
  • 106
  • 140
1

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
0

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'.

Dominique
  • 16,450
  • 15
  • 56
  • 112