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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:39:43+00:00 2026-06-18T10:39:43+00:00

I have a somewhat long awk script and I want to append two columns

  • 0

I have a somewhat long awk script and I want to append two columns at the “right hand side” of the file. I have the following file in progress:

Node    temporary_Temperature   steady_Temperature  temporary_Sight steady_Sight    
1                   x                   x                   -                   -                   
2                   x                   x                   -                   -                   
3                   -                   -                   -                   -                   
4                   -                   -                   -                   -                   
5                   x                   x                   x                   -                   
6                   -                   -                   -                   -                   
7                   -                   -                   -                   -                   
8                   -                   -                   -                   -                   
9                   -                   -                   -                   -                   
10                  -                   -                   -                   -                   
11                  -                   -                   -                   -                   
12                  -                   -                   -                   -                   
13                  x                   x                   -                   -                   
14                  x                   x                   -                   -                   
15                  x                   -                   -                   -                   
16                  -                   -                   -                   -                   

The data are already written to a file and I want to iterate the lines in this file using awk and, let’s say, append two more columns. At the header row I want to append the columns foo and bar, and thereafter append x or - to each line depending on other stuff. How can I achieve this? Since I am in the middle of a longer script I do not want to use sed unless there is some way to invoke sed from within an awk script amd print to the same file?

  • 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-18T10:39:44+00:00Added an answer on June 18, 2026 at 10:39 am

    The problem you may face here is that whenever awk modifies fields, it re-writes your $0, replacing IFS with OFS. If your FS is “any amount of whitespace” (the default FS), and it gets replaced with “a space” (the default OFS), you lose formatting.

    To demonstrate:

    [ghoti@pc ~]$ printf 'one    two\tthree\n'
    one    two      three
    [ghoti@pc ~]$ printf 'one    two\tthree\n' | awk '{$4 = "four"} 1'
    one two three four
    

    If you don’t really want to modify the fields of each line, and you just want to apend a string, you can do that without rewriting your field separators:

    [ghoti@pc ~]$ printf 'one    two\tthree\n' | awk '{print $0 "\tfour"}'
    one    two      three   four
    

    Without seeing your script, or the kind of data you want to append, it’s difficult for me to provide specific examples that I know will be applicable to your situation, but here’s an example to get you started:

      NR == 1 {
        print $0 "\tfoo\tbar";
        next;
      }
      {
        print $0 "\t" (conditionone ? "x" : "-") "\t" (conditiontwo ? "x" : "-");
      }
    

    If you need help crafting the conditions, please include them in your question. I’ve used short-forms in the code above; you could obviously space things out and make multiple-line prints using printf(), or set variables based on the two conditions to be included in your print.

    As an alternative, you could just re-do your formatting as you go through the input data. For example (assuming the trailing spaces in your question are really what your input file looks like):

    BEGIN {
      fmt="%-20s%-20s%-20s%-20s%-20s%-20s%s\n";
    }
    
    NR == 1 {
      printf("%s%12s%-20s%-20s\n", $0, " ", "foo", "bar");
      next;
    }
    
    { foo="-"; bar="-"; }
    
    conditionone { foo="x"; }
    conditiontwo { bar="x"; }
    
    {
      printf(fmt, $1, $2, $3, $4, $5, foo, bar);
    }
    

    You’d probably have to fiddle with your spacing on line 1.

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

Sidebar

Related Questions

I have a somewhat-long-running stored procedure being called from a PB application. I want
I have two somewhat related problems, shown by the following: user=> (if (symbol? 5)
I have following somewhat complex sql query which has horrible performance, 'certainly' due to
I have a somewhat long string that gets truncated when displayed in a tablView
I have a somewhat long-taking WCF-based process. WCF service runs in Azure if its
I have a somewhat long-running post-build event (long enough to be annoying to wait
I have a class model somewhat like this: public class MyModel { long MyModelID
UPDATE: I have somewhat resolved the issue. Just in case if anyone runs in
I need to extract a value from a hidden HTML field, have somewhat figured
Setting up a project structure; I am thinking is it better to have somewhat

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.