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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:43:27+00:00 2026-05-29T08:43:27+00:00

My question might not be quite clear… so lets get to samples datas… fileA

  • 0

My question might not be quite clear… so lets get to samples datas…
fileA
162,400
174,402
175,404
187,406

fileB
E-J1-N5,319319,240248,364364,162162
F-J6-N1,380380,250250,360360,162174
E-J6-N2,380380,240240,364374,175187

Wanted output
E-J1-N5,319319,240248,364364,400400
F-J6-N1,380380,250250,360360,400402
E-J6-N2,380380,240240,364374,404406

So I need to substitute all occurrences of field1 (fileA) by field2 (fileA) but in another file, fileB (field $5).

Here is what I though could work. Store in two variables: recordx (field1) and recordx (field2). My NOT working code:

#!/bin/bash
for record in fileA
   do
        field1=`cut -d, -f 1 $record`
        field2=`cut -d, -f 2 $record`
        awk -v var1=$field1 -v var2=$field2 -F, '{sub(/var1/,"var2",$5);print}' fileB

   done

I get an error :
awk: cannot open 175 (No such file or directory)

Why is awk trying to open a variable ?
Am I heading in a good direction ?

Any help would be greatly 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-05-29T08:43:28+00:00Added an answer on May 29, 2026 at 8:43 am

    awk to the rescue

    awk 'NR==FNR{
           a[$1]=$2
           next
         }
         {
           match($5,/(...)(...)/,b)
           $5=a[b[1]] a[b[2]]
         }1' FS="," OFS="," fileA fileB
    

    Output

    $ awk 'NR==FNR{a[$1]=$2;next}{match($5,/(...)(...)/,b);$5=a[b[1]] a[b[2]]}1' FS="," OFS="," fileA >
    E-J1-N5,319319,240248,364364,400400
    F-J6-N1,380380,250250,360360,400402
    E-J6-N2,380380,240240,364374,404406
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question might not be clear, so i will explain further. I saw some
Ok, the question might not be crystal clear. Let me give some details: Let's
I'm not quite familiar with asp.net ajax, so my question might be a silly
This might not be quite the question you're expecting! I don't want a regex
This question might not seem programming related at first, but let me explain. I'm
Although my question might seem abstract I hope it's not. Suppose I develop an
Before answering, it is not as easy question as you might have thought about
Unfortunately I am not writing this question from my Developing PC so I might
I know this might seem a controversial question but it really is not meant
This might be a stupid question, but I was wondering whether or not you

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.