Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6862139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:37:29+00:00 2026-05-27T02:37:29+00:00

How do I use the awk range pattern ‘/begin regex/,/end regex/’ within a self-contained

  • 0

How do I use the awk range pattern '/begin regex/,/end regex/' within a self-contained awk script?

To clarify, given program csv.awk:

#!/usr/bin/awk -f

BEGIN {
FS = "\"" 
}

/TREE/,/^$/
{
  line="";
        for (i=1; i<=NF; i++) {
                if (i != 2) line=line $i;
        }

        split(line, v, ",");
        if (v[5] ~ "FOAM") {
                print NR, v[5];
        }
}

and file chunk:

TREE
10362900,A,INSTL - SEAL,Revise
,10362901,A,ASSY / DETAIL - PANEL,Revise
,,-203,ASSY - PANEL,Qty -,Add
,,,-309,PANEL,Qty 1,Add
,,,,"FABRICATE FROM TEKLAM NE1G1-02-250 PER TPS-CN-500, TYPE A"
,,,-311,PANEL,Qty 1,Add
,,,,"FABRICATE FROM TEKLAM NE1G1-02-750 PER TPS-CN-500, TYPE A"
,,,-313,FOAM SEAL,1.00 X 20.21 X .50 THK,Qty 1,Add
,,,,"BMS1-68, GRADE B, FORM II, COLOR BAC706 (BLACK)"
,,,-315,FOAM SEAL,1.50 X 8.00 X .25 THK,Qty 1,Add
,,,,"BMS1-68, GRADE B, FORM II, COLOR BAC706 (BLACK)"
,PN HERE,Dual Lock,Add
,
10442900,IR,INSTL - SEAL,Update (not released)
,10362901,A,ASSY / DETAIL - PANEL,Revise
,PN HERE,Dual Lock,Add

I want to have this output:

27 FOAM SEAL
29 FOAM SEAL

What is the syntax for adding the command line form '/begin regex/,/end regex/' to the script to operate on those lines only? All my attempts lead to syntax errors and googling only gives me the cli form.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:37:30+00:00Added an answer on May 27, 2026 at 2:37 am

    Simply do:

    #!/usr/bin/awk -f
    
    BEGIN {
    FS = "\"" 
    }
    
    /from/,/to/ {
      line="";
            for (i=1; i<=NF; i++) {
                    if (i != 2) line=line $i;
            }
    
            split(line, v, ",");
            if (v[5] ~ "FOAM") {
                    print NR, v[5];
            }
    }
    

    If the from to regexes are dynamic:

    #!/usr/bin/awk -f
    
    BEGIN {
        FS = "\"" 
        FROM=ARGV[1]
        TO=ARGV[2]
        if (ARGC == 4) { # the pattern was the only thing, so force read from standard input                   
           ARGV[1] = "-"
        } else {
           ARGV[1] = ARGV[3] 
        }
    }
    
    { if ($0 ~ FROM) { p = 1 ; l = 0} }
    { if ($0 ~ TO)   { p = 0 ; l = 1} } 
    
    {  
      if (p == 1 || l == 1) {
       line="";
            for (i=1; i<=NF; i++) {
                    if (i != 2) line=line $i;
            }
    
            split(line, v, ",");
            if (v[5] ~ "FOAM") {
                    print NR, v[5];
            }
       l = 0 } 
    }
    

    Now you have to call it like: ./scriptname.awk "FROM_REGEX" "TO_REGEX" INPUTFILE. The last param is optional, if missing STDIN can be used.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use awk to join various fields, given that I don't know
I'm trying to use the AWK in a unix shell script to substitue an
I have the following code that uses 'paste' and AWK script inside Perl. use
I usually use awk BEGIN {FS= } NR==FNR{arry[$1]=$0; next} $1 in array && $0=arr[$1]
I have some awk scripts that use gawk from cygwin. Now I need to
I am trying to use awk as some types of element counter as follows,
I'm stil pretty new to regular expression and just started learning to use awk.
I have output with this format: /ignore-this/^/../I/want/this@ignore-this I am trying to use an awk
To cut last field in string I use: command: echo /dir1/dir2/dir3/file | awk -F
I have a small awk script that does some in-place file modifications (to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.