Lemmy
Lemmy

Reputation: 319

Regular expression Bash

I'm writing a program for comparing archives. And I have a problem with editing strings. I try to edit this with regular expressions.

All I need is to edit this line

archive1\sample\nothing.txt

and get only a name of the file without subdirectories. Like that

nothing.txt

I tried to edit a text with this expression, but it seem didn't work.

expr " archive1\sample\nothing.txt" : '\([a-z]*["."]*[a-z]\)'

Upvotes: 1

Views: 52

Answers (2)

Benjamin W.
Benjamin W.

Reputation: 52112

The basename program strips directories from file names:

$ basename 'archive1\sample\nothing.txt'
nothing.txt

Upvotes: 1

fedorqui
fedorqui

Reputation: 289525

Just use \ as a delimiter and print the last block:

$ echo "archive1\sample\nothing.txt" | awk -F"\\" '{print $NF}'
nothing.txt

Or with Bash, use string expressions to remove everything before the last \:

$ r="archive1\sample\nothing.txt"
$ echo ${r##*\\}
nothing.txt

Upvotes: 4

Related Questions