前書き
このリポジトリは、grepコマンドを実行して対象ファイルに記述された文字列に該当する行番号を取得します
インストールする必要のあるコマンド
- git
クイックスタート
上記のコマンドをインストール済みの方は、以下のコマンドを実行してリポジトリからダウンロード後、ディレクトリを移動し、grepコマンドを実行して行番号を取得できます
ubuntu
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
出力結果
3
macos
MacPorts
source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
出力結果
3
windows
WSL2
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
出力結果
3
MSYS2 MINGW64
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
出力結果
3
実行手順
ubuntu
クリックして詳細を開く
事前確認
以下のコマンドを端末に打ち込んでcommand not foundが出なければokです
git --version
preinstall
端末でcommand not foundが出たコマンドを以下のコマンドでインストールしてください
sudo apt install git
コマンド
以下のコマンドを実行することで詳細のような行番号が出力されるはずです
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
詳細
3
macos
クリックして詳細を開く
事前確認
以下のコマンドをターミナルに打ち込んでcommand not foundが出なければokです
source ~/bashrc_folder/macports_alias
git --version
※macosはMacPortsパッケージマネージャを使用してコマンドを管理します。もしインストールしていない方は以下のリンクからMacPortsのインストール手順をご覧ください
またコマンドに別名を設定して既存の環境と競合しないでコマンドを呼び出せるようにします。
初めてこのブログを利用する方は、以下の2つの記事を参考に環境構築してください
preinstall
ターミナルでcommand not foundが出たコマンドを以下のコマンドでインストールしてエイリアスを設定してください
sudo port install git
echo 'alias git="/opt/local/bin/git"' >> ~/bashrc_folder/macports_alias
コマンド
以下のコマンドを実行することで詳細のような行番号が出力されるはずです
source ~/bashrc_folder/macports_alias
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
詳細
3
windows
クリックして詳細を開く
事前確認
以下のコマンドをプロンプトに打ち込んでcommand not foundが出なければokです
WSL2
git --version
MSYS2 MINGW64
git --version
※windowsはWSL2とMSYS2 MINGW64で確認しています。可能な限りWSL2をインストールしていただいて、もし設定できなかった場合はMSYS2をインストールすることで実行できますが、所々WSL2でしか実行できないコマンドが出てくるかもしれません。WSL2とMSYS2のインストール方法は下記の記事を参考にしてください
preinstall
プロンプトでcommand not foundが出たコマンドを以下のコマンドでインストールしてください
WSL2
sudo apt install git
MSYS2 MINGW64
pacman --sync git
コマンド
以下のコマンドを実行することで詳細のような行番号が出力されるはずです
WSL2
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
詳細
3
MSYS2 MINGW64
git clone https://github.com/trygfmi/grep_getRowNumber
cd grep_getRowNumber
cat search_file.txt
grep -n K5m\!P7oR search_file.txt | cut -d: -f1
詳細
3
後書き
記号を検索文字列として使用するときは、\(バックスラッシュ)でエスケープする必要がある時があります。cutコマンドの-dオプションはデリミタで、直後の文字で|(パイプ)で受け取った文字列を分割しています。-f2で検索文字列が取得できるはずです。