grepで「->」をパターンとして使用するにはどうすればよいですか?

grepで「->」をパターンとして使用するにはどうすればよいですか?

次のファイルにこのテキストがあります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

を実行すると、egrep -v 'lrwx' temp最後の5行が削除されます。

私はrunningを実行した後、egrep -v '->' temp同じ5行を削除し、lrwx同じ->行に表示されると予想しました。

ただし、次のエラーが発生します。

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

何の役にも立ちませんでしたegrep -v '-\>' temp

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

egrep(またはを使用しても同じ結果が得られますgrep -E。)

ベストアンサー1

->grepは先行によりオプションとして解釈されます-。 2つの方法があります。

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

記録によると、lsを解析することはシンボリックリンクを見つけるための悪い方法です。 (bashでは)次のような方が良いでしょう:

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

おすすめ記事