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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:15:44+00:00 2026-06-13T14:15:44+00:00

UPDATED QUESTION Ok, so I have a file with lines like this: 44:) 2.884E-02

  • 0

UPDATED QUESTION
Ok, so I have a file with lines like this:

44:)   2.884E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  9.990E+02
45:)   2.884E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  9.990E+02
1:)   3.593E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  1.000E+05
2:)   3.593E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  1.000E+05

The numbers in the first column run from 1 to x (in this case 45) and then starts over at 1 lots of times. I want to move some of the columns to a separate file. The indexes of the columns I want to move is stored in the variable/array $selected_columns (in this case 2, 5 and 8) and the number of columns I want to move is stored in $number_of_columns (in this case 3).

I then want to create 45 files, one for the selected columns for all 1:), one for the selected columns for all 2:) and so forth. I want to make this as general as possible since both the number of columns and the number running from 1 to x will change. The number x is always known and the columns to extract are chosen by the user.

ORIGINAL QUESTION:

I have a string fetched by egrep. Then I want to print some of the columns (words) in that string. The position (column index) is known in a list in my bash script. Currently it looks like this:

line=$(egrep " ${i}:\)" $1)

for ((j=1; j<=$number_of_columns; j++))
do
    awk $line -v current_column=${selected_columns[$j]} '{printf $(current_column)}' > "history_files/history${i}"
done

where number_of_columns is the number of columns that are to be printed and selected_columns contain the corresponding indexes of those columns. As an example number_of_columns = 3 and selected_columns = [2 5 8], so I want to print word number 2, 5 and 8 from the string line to the file history${i}.

I am not sure what is wrong, but this has been done with some trial and error. The current error is awk: cannot open 0.000E+00 (No such file or directory).

Any help is appreciated!

  • 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-13T14:15:45+00:00Added an answer on June 13, 2026 at 2:15 pm

    In:

    awk $line -v ...
    

    $line holds the output of a grep, probably not something awk expects to see on it’s command line. Also,m this:

    for ((j=1; j<=$number_of_columns; j++))
    do
        anything > "history_files/history${i}"
    done
    

    will cause you to overwrite the history file every time through the loop. I don’t know what you really wanted there.

    You have a slew of other issues with your script, though. You said “As an example number_of_columns = 3 and selected_columns = [2 5 8], so I want to print word number 2, 5 and 8 from the string line to the file history${i}.”.

    That’s trivial entirely in awk and you don’t need to do a “grep” outside of awk either, so you could just do the whole thing as:

    awk -v pat=" ${i}:\)" -v selected_columns="$selected_columns" '
    
    BEGIN { number_of_columns = split(selected_columns,selected_columnsA) }
    
    $0 ~ pat {
        sep=""
        for (j=1;j<=number_of_columns;j++) {
            current_column = selected_columnsA[j]
            printf "%s,%s",sep,lineA[current_column]
            sep = "\t"
        }
        print ""
    }
    ' "$1" > "history_files/history${i}"
    

    If that doesn’t work for you, let’s fix THAT instead of trying to fix the original script. Sounds like you have enclosing loop outside of the above, chances are that could just be part of the awk script as well.

    EDIT based on updated OP:

    I’ve added lots of comments but let me know if you have questions:

    $ cat file
    44:)   2.884E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  9.990E+02
    45:)   2.884E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  9.990E+02
    1:)   3.593E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  1.000E+05
    2:)   3.593E-02  0.000E+00  0.000E+00  2.780E+02  0.000E+00  0.000E+00  1.000E+05
    $
    $ cat tst.sh
    selected_columns=(2 5 8)
    
    selCols="${selected_columns[@]}"
    
    awk -v selCols="$selCols" '
    
    BEGIN { # Executed before the first line of the input file is read
    
        # Split the string of selected column numbers, selCols, into
        # an array selColsA where selColsA[1] has the value of the
        # first space-separated sub-string of selCols (i.e. the number
        # of the first column to print). Note that we dont need the
        # number of columns passed into the script as a result of
        # splitting the string is the count of elements put into the
        # array as a return code from the split() builtin function.
        numCols = split(selCols,selColsA)
    }
    
    { # Executed once for every line of the input file
    
        # Create a numerix suffix like "45" from the first column
        # in the current line of the input file, e.g. "45:)" by
        # just getting rid of all non-digit characters.
        sfx = $1
        gsub(/[^[:digit:]]/,"",sfx)
    
        # Create the name of the output file by attaching that
        # numeric suffix to the base value for all output files.
        #histfile = "history_files/history" sfx
        histfile = "tmp" sfx
    
    
        # Loop through every column we want printed. selColsA[<index>]
        # gives us a column number which we can then use to access the
        # columns of the current line. Awk uses the builtin variable $0
        # to hold the current line, and it autolatically splits it so
        # that $1 holds the first column, $2 is the second, etc. So
        # if selColsA[1] has the value 3, then $(selColsA[1]) would be
        # the value of the 3rd column of the current input line.
        sep=""
        for (i=1;i<=numCols;i++) {
            curCol = selColsA[i]
    
            # Print the current column, prefixed by a tab for all but
            # the first column, and without a terminating newline so the
            # next column gets appended to the end of the current output line.
            # Note that in awk "> file" has different semantics from shell
            # and opens the file for writing the first time the line is hit
            # like "> file" in shell, but then appends to it every time its
            # hit afterwards, like ">> file" in shell.
            printf "%s%s",sep,$curCol > histfile
            sep = "\t"
        }
        # Add a newline to the end of the current output line
        print "" > histfile
    }
    
    ' "$1"
    $
    $ ./tst.sh file
    $
    $ cat tmp1
    3.593E-02       2.780E+02       1.000E+05
    $ cat tmp2
    3.593E-02       2.780E+02       1.000E+05
    $ cat tmp44
    2.884E-02       2.780E+02       9.990E+02
    $ cat tmp45
    2.884E-02       2.780E+02       9.990E+02
    

    By the way, I used the words “column” and “line” above for your benefit since you’re just learning, but FYI the awk terminology is actually “field” and “record”.

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

Sidebar

Related Questions

I little while back I posted this question . I have updated that question
I have a fixed format file. I want to access specific lines in this
I have a text file with about 100,000 lines (5 MB), which is updated
Edit (updated question) I have a simple C program: // it is not important
I have been following the answer of this question: How to update existing object
Updated question, see below I'm starting a new project and I would like to
This is probably a simple question, but it's driving me crazy! I have a
DISCLAIMER: This question is only for those who have access to the econometrics toolbox
I have a question! I'm not sure if this is possible, but... I have
This is a follow-up of this question: Ada: reading from a file . I

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.