Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this text in a file called temp:

-rw-r--r-- 1 root root  15776 Oct 15  2010 dnd-copy
-rw-r--r-- 1 root root  15776 Oct 15  2010 dnd-link
-rw-r--r-- 1 root root  15776 Oct 15  2010 dnd-move
-rw-r--r-- 1 root root  15776 Oct 15  2010 dnd-none
-rw-r--r-- 1 root root  15776 Oct 15  2010 dotbox
lrwxrwxrwx 1 root root      5 Oct  8  2012 cross_reverse -> cross
lrwxrwxrwx 1 root root      5 Oct  8  2012 diamond_cross -> cross
lrwxrwxrwx 1 root root      6 Oct  8  2012 dot_box_mask -> dotbox
lrwxrwxrwx 1 root root     17 Oct  8  2012 double_arrow -> sb_v_double_arrow
lrwxrwxrwx 1 root root      9 Oct  8  2012 draft_large -> right_ptr

If I run egrep -v 'lrwx' temp, the last five lines are eliminated.

I expected that running egrep -v '->' temp, would eliminate the same five lines because lrwx and -> occur on the same lines.

However, I get this error:

[09:43 PM] ~/Desktop $ egrep -v '->' temp
egrep: invalid option -- '>'
Usage: egrep [OPTION]... PATTERN [FILE]...
Try 'egrep --help' for more information.

Trying egrep -v '-\>' temp doesn't help either:

[09:46 PM] ~/Desktop $ egrep -v '-\>' temp
egrep: invalid option -- '\'
Usage: egrep [OPTION]... PATTERN [FILE]...
Try 'egrep --help' for more information.

(I get the same results with egrep or grep -E.)

share|improve this question
    
grep -v -- '->' –  Ulrich Dangel Apr 9 '13 at 16:38
    
That worked :) Won't you put it as an answer so that I can accept it? –  user15760 Apr 9 '13 at 16:43
add comment

1 Answer

up vote 4 down vote accepted

-> is being interpreted as an option that grep should interpret for its own use due to the leading -. There are two methods you could use:

grep -- '->' # Explicitly declare end of arguments using --
grep '\->'   # Escape -, which still evaluates to -.

For the record, parsing ls is a bad way of finding symlinks that can quite easily lead to false positives or mangled data, especially if you're going to try and parse this further. Something like this would be better (in bash):

shopt -s nullglob
for file in *; do
    [[ -h $file ]] || continue
    printf '%s -> %s\n' "$file" "$(readlink "$file")"
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.