質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
bash

bash(Bourne-again-Shell)は sh(Bourne Shell)のインプリメンテーションに様々な機能が追加されたシェルです。LinuxやMac OS XではBashはデフォルトで導入されています。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

Q&A

解決済

3回答

2232閲覧

grepで$記号を用いた行末指定が使えない

applepine

総合スコア6

bash

bash(Bourne-again-Shell)は sh(Bourne Shell)のインプリメンテーションに様々な機能が追加されたシェルです。LinuxやMac OS XではBashはデフォルトで導入されています。

MacOS(OSX)

MacOSとは、Appleの開発していたGUI(グラフィカルユーザーインターフェース)を採用したオペレーションシステム(OS)です。Macintoshと共に、市場に出てGUIの普及に大きく貢献しました。

0グッド

0クリップ

投稿2019/09/12 12:35

編集2019/09/12 12:38

前提・実現したいこと

macOSでbashでの正規表現の学習をしています。
grepで$記号を用いた行末指定をしようとしたのですが検索パターンが認識されません。

発生している問題・エラーメッセージ

指定したzen.txtファイルにはidea.という文字列が含まれている行があるにも関わらず$を加えると認識されません。

name-no-MacBook-Pro:Desktop name$ grep idea.$ zen.txt
name-no-MacBook-Pro:Desktop name$

該当のソースコード

macOS bash

name-no-MacBook-Pro:~ name$ export GREP_OPTIONS='--color=always'
name-no-MacBook-Pro:~ name$ cd Desktop
name-no-MacBook-Pro:Desktop name$ grep idea.$ zen.txt
name-no-MacBook-Pro:Desktop name$

試したこと

name-no-MacBook-Pro:Desktop name$ grep idea. zen.txt
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!}

補足情報(FW/ツールのバージョンなど)

ターミナル バージョン2.9.5(421.2)

(zen.txt 中身)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

idea.は行末には無いですね。idea.\が行末です。
よってgrep 'idea.\$' zen.txt でコマンド実行してください。

text

1$ cat zen.txt 2The Zen of Python, by Tim Peters 3 4Beautiful is better than ugly. 5Explicit is better than implicit. 6Simple is better than complex. 7Complex is better than complicated. 8Flat is better than nested. 9Sparse is better than dense. 10Readability counts. 11Special cases aren't special enough to break the rules. 12Although practicality beats purity. 13Errors should never pass silently. 14Unless explicitly silenced. 15In the face of ambiguity, refuse the temptation to guess. 16There should be one-- and preferably only one --obvious way to do it. 17Although that way may not be obvious at first unless you're Dutch. 18Now is better than never. 19Although never is often better than *right* now. 20If the implementation is hard to explain, it's a bad idea.\ 21If the implementation is easy to explain, it may be a good idea.\ 22Namespaces are one honking great idea -- let's do more of those! 23 24$ grep 'idea.\$' zen.txt 25If the implementation is hard to explain, it's a bad idea.\ 26If the implementation is easy to explain, it may be a good idea.\ 27

投稿2019/09/12 13:25

編集2019/09/12 13:31
Y.H.

総合スコア7914

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Y.H.

2019/09/12 13:34

teratailの回答の表示、 '¥'と'\'どういう基準でフォント切り替えされてるんだろう・・・
applepine

2019/09/13 15:47

回答ありがとうございます。ご指摘通りすると意図通りに動きました。
guest

0

ベストアンサー

idea.が行末ではないということですね。

sed 's/$/@@@/' zen.txtしてみてください。@@@の前に何かありませんか?

投稿2019/09/12 12:57

otn

総合スコア84498

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

applepine

2019/09/12 13:07

回答ありがとうございます。 ご指摘いただいたコードを実行したみたのですが、idea.\@@@となっているのですが\までが行末ということでしょうか?grep idea.\ $ zen.txt で実行してみてもやはり認識しません。 何卒初心者なもので知識不足かと思いますので、もう少し詳しく教えていただけないでしょうか? よろしくお願いします。 ``` name-no-MacBook-Pro:Desktop name$ sed 's/$/@@@/' zen.txt {\rtf1\ansi\ansicpg932\cocoartf1671\cocoasubrtf600@@@ {\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}@@@ {\colortbl;\red255\green255\blue255;\red0\green0\blue0;}@@@ {*\expandedcolortbl;;\csgray\c0;}@@@ \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0@@@ \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0@@@ @@@ \f0\fs22 \cf2 \CocoaLigature0 The Zen of Python, by Tim Peters\@@@ \@@@ Beautiful is better than ugly.\@@@ Explicit is better than implicit.\@@@ Simple is better than complex.\@@@ Complex is better than complicated.\@@@ Flat is better than nested.\@@@ Sparse is better than dense.\@@@ Readability counts.\@@@ Special cases aren't special enough to break the rules.\@@@ Although practicality beats purity.\@@@ Errors should never pass silently.\@@@ Unless explicitly silenced.\@@@ In the face of ambiguity, refuse the temptation to guess.\@@@ There should be one-- and preferably only one --obvious way to do it.\@@@ Although that way may not be obvious at first unless you're Dutch.\@@@ Now is better than never.\@@@ Although never is often better than *right* now.\@@@ If the implementation is hard to explain, it's a bad idea.\@@@ If the implementation is easy to explain, it may be a good idea.\@@@ Namespaces are one honking great idea -- let's do more of those!\@@@ }@@@ name-no-MacBook-Pro:Desktop name$ grep idea.$ zen.txt name-no-MacBook-Pro:Desktop name$ ```
otn

2019/09/12 13:36 編集

> \までが行末ということでしょうか? そうでしょうね。 というか、質問文中の、「(zen.txt 中身)」のところは、何を根拠にそう書いたのでしょうか? cat zen.txt したわけではない? > grep idea.$ zen.txt grep 'idea.\$' zen.txt ですね。
applepine

2019/09/12 15:10

>というか、質問文中の、「(zen.txt 中身)」のところは、何を根拠にそう書いたのでしょうか?cat zen.txt したわけではない? テキストファイルを開いてその中身をコピーして判断しました。 cat zen.txt を実行した結果は以下の通りです。 ``` name-no-MacBook-Pro:Desktop name$ cat zen.txt {\rtf1\ansi\ansicpg932\cocoartf1671\cocoasubrtf600 {\fonttbl\f0\fnil\fcharset0 Menlo-Regular;} {\colortbl;\red255\green255\blue255;\red0\green0\blue0;} {*\expandedcolortbl;;\csgray\c0;} \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0 \f0\fs22 \cf2 \CocoaLigature0 The Zen of Python, by Tim Peters\ \ Beautiful is better than ugly.\ Explicit is better than implicit.\ Simple is better than complex.\ Complex is better than complicated.\ Flat is better than nested.\ Sparse is better than dense.\ Readability counts.\ Special cases aren't special enough to break the rules.\ Although practicality beats purity.\ Errors should never pass silently.\ Unless explicitly silenced.\ In the face of ambiguity, refuse the temptation to guess.\ There should be one-- and preferably only one --obvious way to do it.\ Although that way may not be obvious at first unless you're Dutch.\ Now is better than never.\ Although never is often better than *right* now.\ If the implementation is hard to explain, it's a bad idea.\ If the implementation is easy to explain, it may be a good idea.\ Namespaces are one honking great idea -- let's do more of those!\ } ``` >grep 'idea.$' zen.txtですね。 コードの実行結果は以下の通りです。ご指摘の通りやってみましたがやはりうまくいきません。 ``` name-no-MacBook-Pro:Desktop name$ grep 'idea.$' zen.txt name-no-MacBook-Pro:Desktop name$ ```
otn

2019/09/12 15:16

grep 'idea.\$' zen.txt です。 > テキストファイルを開いて 開き方が悪かったんでしょう。
otn

2019/09/12 15:18

正規表現の勉強なら、もっと普通のファイルを使いましょう。
applepine

2019/09/13 15:49

回答ありがとうございます。ご指摘の通りすれば意図どおりに動きました。
guest

0

bash$を変数など特別な意味で解釈しますのでそのまま書いてはgrepコマンドに渡されません。
grep idea.$ zen.txtといった風にエスケープするかgrep 'idea.$' zen.txtといった風に引数全体をシングルコーテーションで囲ってください。

投稿2019/09/12 13:22

編集2019/09/12 13:23
nomuken

総合スコア1627

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

otn

2019/09/12 13:24

$ の次が空白なので、変数とみなされず、大丈夫です。
nomuken

2019/09/12 13:34

そうみたいですね・・・orz それ以外だと実はテキストに\rが混ざってるとか?
applepine

2019/09/13 15:48

回答ありがとうございます。grep 'idea.\$'とすれば意図どおりに動きました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問