我有 3 个文件,全部包含信息列
id.文件
1
2
3
名称.文件
Josh
Kate
Chris
姓氏.文件
Smith
Jones
Black
我想以某种方式将它们结合起来,这样我就可以得到这样的东西:
The ID of the Josh Smith is 1
The ID of the Kate Jones is 2
The ID of the Chris Black is 3
到目前为止,我已经尝试使用粘贴将它们组合起来,paste -d ',' id.file name.file lastname.file
效果很好,但我也想在开头和值之间添加单词。
答案1
单程:
paste name.file lastname.file id.file | awk -F '\t' '{printf "The ID of the %s %s is %d\n", $1,$2,$3}'
用于awk
获取所需的格式。
答案2
另一种可能的解决方案(假设每个文件中始终有单列)是:
paste name.file lastname.file id.file |xargs printf 'the id of the %s %s is %d\n'
或者awk
只有和没有列限制:
awk '{ getline name<"name.file"; getline lastname<"lastname.file"}
{ print "the Id of the", name, lastname, "is", $0 }' OFS=' ' id.file
答案3
使用 Icon 库中的程序(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
生产:
-----
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.
这是在这样的系统上:
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution : Debian 8.11 (jessie)
有关图标粘贴的一些详细信息(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)
有关图标的信息可以在以下位置找到:
https://www2.cs.arizona.edu/icon/
最美好的祝愿...干杯,drl