[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash, grep, and regular expressions



FWIW I've just tried out every possibility that has been suggested by
everyone so far. Some of them work as expected, some of them don't.
There does appear to be a certain amount of weirdness going on, though
less than Freddy is getting.

/tmp/m was a file containing all the suggestions, one per line
/tmp/n was the output file
/tmp/x was a directory containing some example files and directories

Bash version: GNU bash, version 2.05a.0(1)-release (i386-pc-linux-gnu)
              Copyright 2001 Free Software Foundation, Inc.
Kernel:       Linux schnellbox 2.6.6 #1 Sun Dec 12 01:10:00 GMT 2004 i686 GNU/Linux

To run all the suggestions without running into quoting hassles, I
did, from within /tmp/x:

nn=`cat ../m | wc -l`; for x in `seq 1 $nn`; do head -$x ../m | tail -1 > ../cmd; chmod a+x ../cmd; cat ../cmd >> ../n; bash --norc --noprofile -c ../cmd >> ../n; echo ====================== >> ../n; done

I added a plain 'ls -al' to the start of the suggestions file to show
the full listing of /tmp/x.

The suggestions:
ls -al
ls -al | grep -v ' \.\<[a-zA-Z0-9].*\>' # returns everything
ls -al | grep -e '\<[^.][[:alnum:]]'  # returns everything
ls -al | grep -e '\<[.][[:alnum:]]'  # returns an empty set
ls -al | grep -v ' \.'
find -maxdepth 1 |grep -v "^\./\."
ls -a | grep -v '^\.'
ls -al | egrep "\B\."
ls -a1 | egrep -v '^\.'
ls -la | egrep ':[[:digit:]]{2} [^.]'
ls -la | awk '$8 ~ /^[^.]/{print}' 
ls -la | awk '{ if (substr($9,0,1) != ".") {print $9}}'
ls -al | grep -e ^d | grep -e '[.][a-z]'
ls -al | grep -e ^d | grep -e '[^.][a-z]'
ls -al | grep -e ^d | grep -e ':[0-9][0-9] [^\.][a-zA-Z]'
ls -al | grep -e ^d | grep -e '\<[^.][a-z]'
ls -la | grep -v -w "\..*" 

The results:
ls -al
total 32
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile2.txt
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
ls -al | grep -v ' \.\<[a-zA-Z0-9].*\>' # returns everything
total 32
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
ls -al | grep -e '\<[^.][[:alnum:]]'  # returns everything
total 32
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile2.txt
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
ls -al | grep -e '\<[.][[:alnum:]]'  # returns an empty set
======================
ls -al | grep -v ' \.'
total 32
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
find -maxdepth 1 |grep -v "^\./\."
.
./file1
./file2.txt
./dir1
./dir2.dir
======================
ls -a | grep -v '^\.'
dir1
dir2.dir
file1
file2.txt
======================
ls -al | egrep "\B\."
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile2.txt
======================
ls -a1 | egrep -v '^\.'
dir1
dir2.dir
file1
file2.txt
======================
ls -la | egrep ':[[:digit:]]{2} [^.]'
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
ls -la | awk '$8 ~ /^[^.]/{print}' 
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 .hiddenfile2.txt
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================
ls -la | awk '{ if (substr($9,0,1) != ".") {print $9}}'

.
..
.hiddendir1
.hiddendir2.dir
.hiddenfile1
.hiddenfile2.txt
dir1
dir2.dir
file1
file2.txt
======================
ls -al | grep -e ^d | grep -e '[.][a-z]'
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
======================
ls -al | grep -e ^d | grep -e '[^.][a-z]'
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
======================
ls -al | grep -e ^d | grep -e ':[0-9][0-9] [^\.][a-zA-Z]'
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
======================
ls -al | grep -e ^d | grep -e '\<[^.][a-z]'
drwxr-xr-x    6 pigeon   pigeon       4096 Feb 18 19:05 .
drwxrwxrwt    3 root     root        12288 Feb 18 19:27 ..
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 .hiddendir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:05 .hiddendir2.dir
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
======================
ls -la | grep -v -w "\..*" 
total 32
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir1
drwxr-xr-x    2 pigeon   pigeon       4096 Feb 18 19:04 dir2.dir
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file1
-rw-r--r--    1 pigeon   pigeon          0 Feb 18 19:04 file2.txt
======================

-- 
Pigeon

Be kind to pigeons
Get my GPG key here: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x21C61F7F

Attachment: signature.asc
Description: Digital signature


Reply to: