ashish_k
ashish_k

Reputation: 1581

Unable to get the output using regex

This might be a silly one, but I'm unable to get the required output.

I've a file named Abc_sfgf_kjwefgk.txt20180929_040845 in a directory.

I'm trying to list the file using the command: ls -l Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}

But it's giving me error: ls: cannot access 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}': No such file or directory

While, with this command:ls -l Abc_sfgf_kjwefgk.txt[0-9]*?_[0-9]*?

It's giving me the correct result.

What's the problem with my initial command?

Upvotes: 0

Views: 129

Answers (2)

Arfeo
Arfeo

Reputation: 867

Seems like everything is okay with your SECOND command. Here you correctly use globbing.

If you want to parse ls, then you should use smth like this:

ls | grep -E 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}'

Upvotes: 1

Socowi
Socowi

Reputation: 27225

{8} is a literal string. Since you don't have any file names containing { the glob regex [0-9] does not expand. Your command is literally ls -l 'Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}'. Since there is no file Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4} you get an error message from ls, that there is no such file.

You probably wanted to write

ls -l Abc_sfgf_kjwefgk.txt[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]

or

find -maxdepth 1 -regextype egrep -regex '.*/Abc_sfgf_kjwefgk.txt[0-9]{8}_[0-9]{4}' \
     -exec ls -l {} \;

Upvotes: 1

Related Questions