grep -o and display part of filenames using ls

I have a directory which has many directories inside it with the pattern of their name as : YYYYDDMM_HHMISS Example: 20140102_120202 I want to extract only the YYYYDDMM part. I tried ls -l|awk '{print $9}'|grep -o ^[0-9]* and got the answer. However i have following questions:

  1. Why doesnt this return any results: ls -l|awk '{print $9}'|grep -o [0-9]* . Infact it should have returned all the directories. Strangely just including '^' before [0-9] works fine : ls -l|awk '{print $9}'|grep -o ^[0-9]*

  2. Any other(simpler) way to achieve the result?

Answers


Why doesnt this return any results: ls -l|awk '{print $9}'|grep -o [0-9]*

If there are files in your current directory that start with [0-9], then the shell will expand them before calling grep. For example, if I have two files a1, a2 and a3 and run this:

ls | grep a*

After the filenames are expanded, the shell will run this:

ls | grep a1 a2 a3

The result of which is that it will print the lines in a2 and a3 that match the text "a1". It will also ignore whatever is coming from stdin, because when you specify filenames for grep (2nd argument and beyond), it will ignore stdin.

Next, consider this:

ls | grep ^a*

Here, ^ has no special meaning to the shell, so it uses it verbatim. Since I don't have filenames starting with ^a, it will use ^a* as the pattern. If I did have filenames like ^asomething or ^another, then again, ^a* would be expanded to those filenames and grep would do something I didn't really intend.

This is why you have to quote search patterns, to prevent the shell from expanding them. The same goes for patterns in find /path -name 'pattern'.

As for a simpler way for what you want, I think this should do it:

ls | sed -ne 's/_.*//p'

To show only the YYDDMM part of the directory names:

for i in ./*; do echo $(basename "${i%%_*}"); done

Not sure what you want to do with it once you've got it though...


You must avoid parsing ls output.

Simple is to use this printf:

printf "%s\n" [0-9]*_[0-9]*|egrep -o '^[0-9]+'

Need Your Help

XML DOM parsing with Java

java xml dom parsing

I'm trying to parse this XML string: