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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:50:46+00:00 2026-05-29T05:50:46+00:00

This is the script I’m trying to add a last argument to that prints

  • 0

This is the script I’m trying to add a last argument to that prints the FNR into the fourth field.

#!/usr/bin/awk -f

{ sub(/\r$/,"") }

/^BEGIN_DATA_FORMAT/{
        getline
            for (i=1;i<=NF;i++) 
                    if ($i~/LAB/) a[i]=$i
                }


/^BEGIN_DATA$/,/^END_DATA$/{
             s="";
             if (NF<2) next; else 
                for (j in a)
            s=s?s"\t"$j:$j
            print s 
            }

This is what the output from this script looks like:

48.34   -55.88  19.19
26.95   24.36   13.43
25.53   4.45    -20.68
71.27   6.68    24.28
...

This is my second script:

#!/usr/bin/awk -f

{ OFS = "\t"; $4="(Untitled "FNR-1")"; print $0 }

Piping the first script into the second script returns the intended result with the FNR starting from 0 in the fourth field.

48.34   -55.88  19.19   (Untitled 0)
26.95   24.36   13.43   (Untitled 1)
25.53   4.45    -20.68  (Untitled 2)
71.27   6.68    24.28   (Untitled 3)
...

I tried combining the scripts but I don’t get the output I’m trying for.

#!/usr/bin/awk -f

{ sub(/\r$/,"") }

/^BEGIN_DATA_FORMAT/{
        getline
            for (i=1;i<=NF;i++) 
                    if ($i~/LAB/) a[i]=$i
                }


/^BEGIN_DATA$/,/^END_DATA$/{
             s="";
             if (NF<2) next; else 
                for (j in a)
            s=s?s"\t"$j:$j
            print s 
                        }

    { 
        OFS = "\t"
        $4="(Untitled "FNR-1")"
        print $4 
    }

This is the output given. The problem is that it’s referencing the same file instead of the output from the last command.

(Untitled 0)
(Untitled 1)
(Untitled 2)
(Untitled 3)
(Untitled 4)
(Untitled 5)
(Untitled 6)
(Untitled 7)
(Untitled 8)
(Untitled 9)
(Untitled 10)
(Untitled 11)
(Untitled 13)
(Untitled 14)
(Untitled 15)
48.34   -55.88  19.19
(Untitled 17)
26.95   24.36   13.43
(Untitled 18)
25.53   4.45    -20.68
(Untitled 19)
71.27   6.68    24.28
(Untitled 20)
...

I’ve also tried this:

#!/usr/bin/awk -f

{ sub(/\r$/,"") }

/^BEGIN_DATA_FORMAT/{
        getline
            for (i=1;i<=NF;i++) 
                    if ($i~/LAB/) a[i]=$i
                }


/^BEGIN_DATA$/,/^END_DATA$/{
             s="";
             if (NF<2) next; else 
                for (j in a)
            s=s?s"\t"$j:$j

        OFS = "\t"
        $4="(Untitled "FNR-1")"
        print s OFS $4 
            }

The output is closer but the problem is that it’s still counting from the FNR of that argument. I need it to start at 0.

48.34   -55.88  19.19   (Untitled 17)
26.95   24.36   13.43   (Untitled 18)
25.53   4.45    -20.68  (Untitled 19)
71.27   6.68    24.28   (Untitled 20)
...

Could someone show me the correct way to combine these scripts?

  • 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-29T05:50:47+00:00Added an answer on May 29, 2026 at 5:50 am

    Just use an incrementing variable rather than the record number:

    print s, "(Untitled " count++ ")"
    

    You should define OFS in the BEGIN block instead of redefining it for each line.

    Instead of doing { sub(/\r$/,"") } why don’t you use “dos2unix” on your file first?

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

Sidebar

Related Questions

This script is giving error if name field and file field is same. That
In this script #!/usr/bin/perl -w use strict; my @ar = (1,2,10,3,5); @ar = sort
Well basically I have this script that takes a long time to execute and
I want to execute this script (view source) that uses Google Translate AJAX API
I have this script that creates a new tab in JQuery. Can you check
This script strips the timezone from an email's Date fields: #!/bin/sed -rf s/(^Date: (Sun|Mon|Tue|Wed|Thu|Fri|Sat),.*[0-9][0-9]:[0-9][0-9]:[0-9][0-9])
Imagine this Script: var s = TStrings.Create; s.Add('Line 1'); s.Add('Line 2'); procedure MyProc; begin
This script basically turns .less code into .css code. I think it will only
This script is supposed to get the current popularity from Mysql, add one digit
This script below works on a field by field basis, and removes the quotes

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.