Reputation: 4084
I have a very simple grep problem but can't seem to solve it. I have several files like this:
sample.txt
sample7.doc.txt
another_sample.sample.lst.txt
three.txt
...And I just want to grab everything before the ".txt". I was trying to do this in shell script like this:
ame=`echo $1 | grep -Po "^[A-Za-z0-9]+"`
...But of course that returns only the portion up until the first 'dot'. Can someone please help modify this regex?
Upvotes: 2
Views: 1886
Reputation: 1231
mv $1 $(echo $1 | awk -F.txt '{print $1}')
Upvotes: 0
Reputation: 45652
No need for regexp:
$ basename sample.txt .txt
sample
or using any POSIX-compatible shell:
$ echo "$a"
sample.txt
$ y=${a%.txt}
$ echo "$y"
sample
Upvotes: 7
Reputation: 1134
remove .txt
name=echo $1|sed 's/.txt$//'
use basename
name=echo $fullname|basename .txt
Upvotes: 0