# Awk (Searching Pattern)

<pre class="language-bash"><code class="lang-bash">#!/bin/bash

# Define the word to search for
SEARCH_WORD="your_word"

# Basic Search with awk:
# Searches for the specified word in all files within the current directory and prints the matching lines.
awk "/$SEARCH_WORD/" ./*

# Search with Line Numbers:
# Searches for the word and prints matching lines with their line numbers.
awk "/$SEARCH_WORD/{print FNR, \$0}" ./*

#-F fs
#-F specifies the field separator,
# where fs can be a single character or a regular expression.
awk -F, '{print $1, $2}' file.csv


# Case-Insensitive Search:
# Searches for the word ignoring case and prints matching lines.
awk 'tolower($0) ~ tolower("'"$SEARCH_WORD"'")' ./*

# Search Specific File Types (e.g., .txt files):
# Limits the search to files with a specific extension or pattern.
<strong>awk "/$SEARCH_WORD/" ./*.txt
</strong>
# Exclude Specific Directories (requires find command):
# First, find all files excluding the specified directory, then search with awk.
find . -type f ! -path "./dir_to_exclude/*" -exec awk "/$SEARCH_WORD/" {} +

# Search and Print File Name, Line Number, and Matching Line:
# A more complex command that prints the filename, line number, and the matching line.
awk "/$SEARCH_WORD/{print FILENAME, FNR, \$0}" ./*

# Case-Insensitive Search and Print Filename, Line Number, and Line:
# Combines case-insensitive search with filename and line number output.
awk 'tolower($0) ~ tolower("'"$SEARCH_WORD"'") {print FILENAME, FNR, $0}' ./*

</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://n0m4dsec.gitbook.io/sec-book/some-important-linux-commands/awk-searching-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
