0

File_1

Group   Country Lang
IT      USA     ENG

File_2

EMPID   STOREID
1001    1400
1002    1401
1003    1401

If I use

paste -d'\t' File_1 File_2

I get

Group^ICountry^ILang^IEMPID^ISTOREID$
IT^IUSA^IENG^I1001^I1400$
^I1002^I1401$   
^I1003^I1401$

While I am trying to get something like this

Group   Country Lang    EMPID   STOREID
IT  USA ENG     1001    1400
IT  USA ENG     1002    1401
IT  USA ENG     1003    1401

Please suggest

1
  • No thats output from vi with :list option Commented Nov 16, 2020 at 20:58

3 Answers 3

2
$ awk -v OFS='\t' 'NR==FNR{a[FNR==1]=$0; next} {print a[FNR==1], $0}' file1 file2
Group   Country Lang    EMPID   STOREID
IT      USA     ENG     1001    1400
IT      USA     ENG     1002    1401
IT      USA     ENG     1003    1401

The above is just using the 1 or 0 result of a comparison "is this the first line of the file" (FNR==1) to index the array so you get the index 1 associated with the first line from file 1 when when it's the first line from file 2 and the index 0 associated with the 2nd line from file 1 for every other line of file 2. It'll behave the same way in any awk.

0
0

You can use awk for this task.

awk '
    FNR == NR {buf[NR] = $0; next} 
    {print (FNR==1 ? buf[1] : buf[2]) "\t" $0}
' file1 file2

Save the header and the row while parsing the first file (FNR==NR) and print the appropriate prefix together with every row of the second file.

Output:

Group   Country Lang    EMPID   STOREID
IT      USA     ENG 1001    1400
IT      USA     ENG 1002    1401
IT      USA     ENG 1003    1401
0

Try this one-liner with GNU awk:

$ awk 'FNR==NR {a[NR]=$0;nrow=FNR;next} NR==nrow+1 {print a[1],$0;next} {print a[2],$0}' data1 data2

Group   Country Lang EMPID   STOREID
IT      USA     ENG 1001    1400
IT      USA     ENG 1002    1401
IT      USA     ENG 1003    1401

with:

$ cat data1
Group   Country Lang
IT      USA     ENG
$ cat data2
EMPID   STOREID
1001    1400
1002    1401
1003    1401

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.