Skip to content
  • About
  • Courses
  • ResearchExpand
    • Research Publications
    • Books
    • Patents
  • Workshop/Conferences
  • ToolsExpand
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
One Page CV
Unix

awk Passing variables

Passing variables to AWK

AWK has the -v option which is used to assign values to variables. For the next program, we have the general file:

$cat general
The French nation, oppressed, degraded during many centuries
by the most insolent despotism, has finally awakened to a 
consciousness of its rights and of the power to which its 
destinies summon it.

mygrep.awk

{
    for (i=1; i<=NF; i++) {

        field = $i
        
        if (field ~ word) {
            c = index($0, field)
            print NR "," c, $0
            next
        }
    }
}

The example simulates the grep utility. It finds the provided word and prints its line and the its starting index. (The program finds only the first occurrence of the word.) The word variable is passed to the program using the -v option.

$ awk -f mygrep.awk -v word=the general
2,4 by the most insolent despotism, has finally awakened to a 
3,36 consciousness of its rights and of the power to which its 

We have looked for the โ€œtheโ€ word in the text file.

Post navigation

Previous Previous
awk more
NextContinue
awk Pipes
  • Latest

    [professional_resume]

    Read More Continue

  • Latest

    Explore Generative AI with the Gemini API in Vertex AI

    Read More Explore Generative AI with the Gemini API in Vertex AIContinue

  • Latest

    Inspect Rich Documents with Gemini Multimodality and Multimodal RAG

    Read More Inspect Rich Documents with Gemini Multimodality and Multimodal RAGContinue

  • Latest

    ๐ŸŽ“ Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)

    In engineering colleges across the country, final year projects are often treated as just another academic task. But what many students fail to realize is…

    Read More ๐ŸŽ“ Why Original Work Matters in Your Final Year Project (And How It Can Shape Your Career)Continue

  • Latest

    ๐ŸŽ“ How to Choose Your Final Year Project: A Practical Guide for BTech Students

    Choosing the right final year project is one of the most important decisions of your engineering journey. Itโ€™s more than just a submission โ€” itโ€™s…

    Read More ๐ŸŽ“ How to Choose Your Final Year Project: A Practical Guide for BTech StudentsContinue

Nishant Munjal

Coding Humanityโ€™s Future </>


Facebook Twitter Linkedin YouTube Github Email

Tools

  • SIP Calculator
  • EMI Calculator
  • Creative QR Code
  • Image Converter

Resources

  • Blog
  • Contact
  • Refund and Returns

Legal

  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

© 2025 - All Rights Reserved

  • About
  • Courses
  • Research
    • Research Publications
    • Books
    • Patents
  • Workshop/Conferences
  • Tools
    • Creative Image Converter
    • Creative QRCode Generator
    • Creative QR Code Generator Tool
    • EMI Calculator
    • SIP Calculator
  • Blog
  • Resume
Download CV
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.