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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:11:44+00:00 2026-06-07T23:11:44+00:00

I am writing a shell script program in which I am internally calling an

  • 0

I am writing a shell script program in which I am internally calling an awk script. Here is my script below.

for FILE in `eval echo{0..$fileIterator}`
{

if(FILE == $fileIterator)
{
    printindicator =1;
}
    grep RECORD FILEARRAY[FILE]| awk 'for(i=1;i<=NF;i++) {if($i ~ XXXX) {XARRAY[$i]++}} END {if(printIndicator==1){for(element in XARRAY){print element >> FILE B}}'

I hope I am clear with my code . Please let me know if you need any other details.

ISSUE

My motivation in this program is to traverse through all the files an get the lines that has “XXXX” in all the files and store the lines in an array. That is what I am doing here. Finally I need to store the contents of the array variable into a file. I can store the contents at each and every step like the below

{if($i ~ XXXX) {XARRAY[$i]++; print XARRAY[$i] >> FILE B}}

But the reason behind not going to this approach is each time I need to do an I/O operation and for this the time taken is much and that is why I am converting that into inmemory everytime and then at last dumping the in memory array(XARRAY) into the file.

The problem I am facing here is that. The shell script calls the awk everytime, the data’s are getting stored in the array(XARRAY) but for the next iteration, the previous content of XARRAY is getting deleted and it puts the new content as this assumes this as a new array. Hence at last when I print the contents, it prints only the lately updated XARRAY and not all the data that is expected from this.

SUGGESTIONS EXPECTED

1) How to make the awk script realize that the XARRAY is an old one and not the new one when it is being called everytime in each iteration.

2) One of the alternative is to do an I/O everytime. But I am not interested in this. Is there any other alternative other than this. Thank you.

  • 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-07T23:11:46+00:00Added an answer on June 7, 2026 at 11:11 pm

    Ouch, can’t tell if it is meant to be real or pseudocode!

    You can’t make awk preserve state. You would either have to save it to a temporary file or store it in a shell variable, the contents of which you’d pass to later invocations. But this is all too much hassle for what I understand you want to achieve.

    I suggest you omit the loop, which will allow you to call awk only once with just some reordering. I assume FILE A is the FILE in the loop and FILE B is something external. The reordering would end up something very roughly like:

    grep RECORD ${FILEARRAY[@]:0:$fileIterator} | awk 'for(i=1;i<=NF;i++) {if($i ~ XXXX) {XARRAY[$i]++}} END {for(element in XARRAY){print element >> FILEB}'
    

    I move the filename expansion to the grep call and removed the whole printIndicator check.

    It could all be done even more efficiently (the obvious one being removal of grep), but you provided too little detail to make early optimisation sensible.

    EDIT: fixed the loop iteration with the info from the update. Here’s a loopy solution, which is immune to new whitespace issues and too long command lines:

    for FILE in $(seq 0 $fileIterator); do
      grep RECORD "${FILEARRAY[$FILE]}"
    done | 
    awk 'for(i=1;i<=NF;i++) {if($i ~ XXXX) {XARRAY[$i]++}} END {for(element in XARRAY){print element >> FILEB}'
    

    It still runs awk only once, constantly feeding it data from the loop.

    If you want to load the results into an array UGUGU, do the following as well (requires bash 4):

    mapfile UGUGU < FILEB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a shell script in which I have to find the last
I am writing a shell script (which I suck at) and I need some
I'm writing a shell script that makes sure my DNS server is looking. Here's
I'm writing a shell script that outputs all untranslated strings from gettext .po file,
I am writing a shell script in which i need to call a binary
I'm writing a shell script which iterates over a set of variables, edits a
I'm writing a shell script to parse through log file and pull out all
I am writing a shell script that takes file paths as input. For this
Writing a shell script that pretty much wraps around an Awk script. I'd like
I am writing a bash shell script in Linux, this program will accept 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.