0

I have to 3 files with all containing columns of information

id.file

1
2
3

name.file

Josh
Kate
Chris

lastname.file

Smith
Jones
Black

And I would like to combine them in a way, so I can get something like this:

The ID of the Josh Smith is 1
The ID of the Kate Jones is 2
The ID of the Chris Black is 3

So far I have tried to combine them using paste paste -d ',' id.file name.file lastname.file which works well but I want to add words in the beginning and between values as well.

3

One way:

paste name.file lastname.file id.file | awk -F '\t' '{printf "The ID of the %s %s is %d\n", $1,$2,$3}'

Using awk to get the formatting needed.

  • Your example works really well, can I ask what do multiple %s and %d do? – user222776 Apr 22 at 13:50
  • 1
    The %s %s and %d gets replaced with first name($1), last name($2) and id($3) – Guru Apr 22 at 13:58
2

Another possible solution (assuming there is always single column in each files) would be:

paste name.file lastname.file id.file |xargs printf 'the id of the %s %s is %d\n'

Or with awk only and with no column limitation:

awk '{ getline name<"name.file"; getline lastname<"lastname.file"}
     { print "the Id of the", name, lastname, "is", $0 }' OFS=' ' id.file
0

Using a program from the Icon library (a symbol-manipulation language in the sense of SNOBOL):

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Input data files" data?
head data?

pl " Expected output:"
head -v $E

# Insert strings like:
# The ID of the Josh Smith is 1
pl " Results:"
icon-paste "-The ID of the " data2 "- " data3 "- is " data1 |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C f1 "$E" || ( pe; pe " Results cannot be verified." ) >&2

producing:

-----
 Input data files data1 data2 data3
==> data1 <==
1
2
3

==> data2 <==
Josh
Kate
Chris

==> data3 <==
Smith
Jones
Black

-----
 Expected output:
==> expected-output <==
The ID of the Josh Smith is 1
The ID of the Kate Jones is 2
The ID of the Chris Black is 3

-----
 Results:
The ID of the Josh Smith is 1
The ID of the Kate Jones is 2
The ID of the Chris Black is 3

-----
 Verify results if possible:

-----
 Comparison of 3 created lines with 3 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output have same content.

This was on a system like:

OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 

Some details on icon-paste (lam.icn):

icon-paste      paste, join, laminate lines from files. (man)
Path    : ~/executable/icon-paste
Version : - ( local: ~/executable/icon-paste, 2012-02-11 )
Length  : 24 lines
Type    : POSIX shell script executable (binary data)
Shebang : #!/bin/sh
Home    : https://www2.cs.arizona.edu/icon/library/src/progs/lam.icn (doc)

Information on Icon can be found at:

https://www2.cs.arizona.edu/icon/

Best wishes ... cheers, drl

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.