Home > Back-end >  Inserting into a column in file
Inserting into a column in file

Time:04-10

I have two files and first one is comma separated file with 6 columns and 3rd column is empty in it. 2nd file is a list of urls.

How would i insert all values from 2nd file in 3rd column of first file ?

Content of first file Ex :

apple,fruits,,healthy,240
grapes,fruits,,healthy,80
Noodles,junk,,unhealthy,80

Content of 2nd file:

httpsxhhscom
httpseightcom
httpsxyzjcom

Each files have same number of records and need to be mapped one to one while inserting.ex: 1st row in 2nd file goes 3rd column of 1st file.

CodePudding user response:

You can do it with awk simply by reading the second file into an index array and then updating the 3rd field in file1. For example:

awk -F, -v OFS="," 'FNR==NR {a[  n]=$1; next} m<=n {$3=a[  m]}1' file2 file1

Explanation

  • the option -F, sets the field separator variable (FS) to ",",
  • the option -v OFS="," sets the output field separator variable (OFS) to ",",
  • FNR == NR (first rule condition) - the file record number (FNR - line number per-file) is equal to the total number of records processed (NR) -- only true for the first file processed by awk. FNR is reset to 1 for each file read, NR continues to increment.
  • If conditon is true, store the url from the first file processed (file2) in the indexed array a[] with indexes 1, 2, 3, .... next skips to the next record (line) at that point.
  • m <= n second rule condition - this will only be reached when processing the second file (file1). The 3rd field is updaded to the url stored in array a[] in order until the end of the second file or when m > n (if there are more records in the second file than the first)
  • Note: m and n are simple variables used as counters for the array index. In awk when a variable is encountered before it is initialized or assigned a value, it's value is 0, so n simple sets n = 1 the first time it is used. When m <= n is encountered before m has been incremented, it's value is 0.
  • 1 at the end is just shorthand for the default operation print.

Example Use/Output

With your data in file1 and file2, you would get:

$ awk -F, -v OFS="," 'FNR==NR {a[  n]=$1; next} m<=n {$3=a[  m]}1' file2 file1
apple,fruits,httpsxhhscom,healthy,240
grapes,fruits,httpseightcom,healthy,80
Noodles,junk,httpsxyzjcom,unhealthy,80

To write that out to a file, simply redirect the result to a new file, e.g. > newfile.

CodePudding user response:

If file2 isn't gigantic, might as well pre-read it all :

mawk 'BEGIN {                                 RS="^$"
               getline <ARGV[  _]
     delete __[split ($(ARGV[_  -!  _]=""),__,RS="\n")] 
         FS = \
        OFS = "," } $_=__[NR]' testf2.txt testf1.txt

apple,fruits,httpsxhhscom,healthy,240
grapes,fruits,httpseightcom,healthy,80
Noodles,junk,httpsxyzjcom,unhealthy,80

Tested and confirmed working on gawk 5.1.1, mawk 1.3.4, mawk 1.996, and macos nawk

—- The 4Chan Teller

  • Related