DEV Community

mich0w0h
mich0w0h

Posted on

Linux search: find a word next to a specific phrase

What I want to achieve

  • Search a word next to a specific phrase in a file with the Linux shell command

How to tackle

Use the grep command and the awk command.

$ grep -w "search_word" file_name | awk '{print $2}'
Enter fullscreen mode Exit fullscreen mode

Explanations

  • by the -w option grep matches only whole words, preventing partial matches
  • {print $2} with awk prints the second word on each line

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs