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 8530535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:16:12+00:00 2026-06-11T09:16:12+00:00

i have an awk script that i’m running against a pair of files. i’m

  • 0

i have an awk script that i’m running against a pair of files. i’m calling it like this:

awk -f script.awk file1 file2

script.awk looks something like this:

BEGIN {FS=":"}
{ if( NR == 1 )
    { 
      var=$2
      FS=" "
    }
   else print var,"|",$0
}

the first line of each file is colon-delimited. for every other line, i want it to return to the default whitespace file seperator.

this works fine for the first file, but fails because FS is not reset to : after each file, because the BEGIN block is only processed once.

tldr: is there a way to make awk process the BEGIN block once for each file i pass it?

i’m running this on cygwin bash, in case that matters.

  • 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-06-11T09:16:13+00:00Added an answer on June 11, 2026 at 9:16 am

    If you’re using gawk version 4 or later there’s the BEGINFILE block. From the manual:

    BEGINFILE and ENDFILE are additional special patterns whose bodies are executed before reading the first
    record of each command line input file and after reading the last record of each file. Inside the BEGINFILE
    rule, the value of ERRNO will be the empty string if the file could be opened successfully. Otherwise, there
    is some problem with the file and the code should use nextfile to skip it. If that is not done, gawk produces
    its usual fatal error for files that cannot be opened.

    For example:

    touch a b c
    awk 'BEGINFILE { print "Processing: " FILENAME }' a b c
    

    Output:

    Processing: a
    Processing: b
    Processing: c
    

    Edit – a more portable way

    As noted by DennisWilliamson you can achieve a similar effect with FNR == 1 at the beginning of your script. In addition to this you could change FS from the command-line directly, e.g.:

    awk -f script.awk FS=':' file1 FS=' ' file2
    

    Here the FS variable will retain whatever value it had previously.

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

Sidebar

Related Questions

I'have a bash script calling a jar file like this: awk -f fileformat.awk list.txt
I have a script that runs in AIX ksh that looks like this: wc
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have a Rails log that looks like this Started GET [path-omitted] for 166.137.138.210
I have a script that looks for files on a mass filer device. Every
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
I have an awk script that processes a csv file and produces a report
I have a shell script that constructs an awk program as a string then
I have this nifty little script that does a nice job of manipulating some
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.