0

I have two files File-1 and File-2 with data in columns. I want to create another file and copy the data of the first column of File-2 corresponding to the data to file-1 in row wise as given below.

File-1



A  P
B  Q
C  R


File-2



5.4  9.0
7.4  11.5
9.4  15.7

Output

A=5.4, B=7.4, C=9.4

I want my output in a row form like above only for first column data.
I appreciate your help!!
Thanks in advance!!!

1
$ paste file1 file2 | awk '{printf "%s%s=%s", sep, $1, $3; sep=", "} END{print ""}'
A=5.4, B=7.4, C=9.4
1
  • wow great!!! thanks @Ed Morton – sai Jan 13 at 6:44
3

Note that a "join" (mentioned in the question title) is a specific operation where one data set is used to extract data from another data set (a relational join operation). This may be done with the join utility (or with a short awk command).

What you want to do is a data extraction and a merge. This is well explained by you in the question text.

Assuming you have the bash shell, the contents of the first column of each file can be extracted using cut, and then merged with = as a delimiter using paste:

$ paste -d '=' <(cut -d ' ' -f 1 File-1) <(cut -d ' ' -f 1 File-2)
A=5.4
B=7.4
C=9.4

Each <(...) (process substitution) will be replaced with the name of a temporary file from where the output of the cut command inside the substitution will be available for paste to read.

You can then merge this into a single comma-delimited line like so:

$ paste -d '=' <(cut -d ' ' -f 1 File-1) <(cut -d ' ' -f 1 File-2) | paste -d ',' -s -
A=5.4,B=7.4,C=9.4

If you require spaces after each comma, sed may be used:

$ paste -d '=' <(cut -d ' ' -f 1 File-1) <(cut -d ' ' -f 1 File-2) | paste -d ',' -s - | sed 's/,/, /g'
A=5.4, B=7.4, C=9.4

Note that I have assumed that the empty lines in the question is not actually part of the data files. You may want to pre-process the data with sed '/^[[:blank:]]*$/d', which would delete any lines that are empty or that only contains spaces and/or tabs.

See also the manual for cut and paste (man cut and man paste).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.