- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下文件(下划线代表制表符分隔符,文件名不包含在文件内容中):
(sample001.文件)
Name_____scores_____gender
Joey_____54_____Boy
Kyle_____87_____Girl
Sia______43_____Girl
Marge____87_____Girl
(sample002.文件)
Name_____scores_____gender
Joey_____23_____Boy
Pedro____76_____Boy
Kyle_____76_____Girl
(sample003.文件)
Name_____scores_____gender
Kyle_____34_____Girl
James____65_____Boy
Pedro____76_____Boy
Sia______65_____Girl
Marge____23_____Girl
我希望将所有这些文件集成到一个文件中,仅包含第一列和第二列数据。它看起来像这样:
(集成.文件)
Name_____sample001____sample002_____sample003
Joey_____54_____23____0
Kyle_____87_____76____34
Sia______43_____0_____65
Marge____87_____0_____23
Pedro____0______76____76
James____0______0_____65
基本上,名称在第一列中应该只有一个条目,如果任何样本都没有数据,则它应该为零。 header 不是必需的,但可以存在。
谁能帮我解决这个问题吗?
最佳答案
使用 Bash 和 process substitution ,您可以在单个(相当长的)命令管道中对三个文件执行此操作:
join -e 0 -a 1 -a 2 -t $'\t' -o 0,1.2,2.2 \
<(sed 1d sample001.file | sort) \
<(sed 1d sample002.file | sort) |
join -e 0 -a 1 -a 2 -t $'\t' -o 0,1.2,1.3,2.2 \
- <(sed 1d sample003.file | sort)
请注意,join
要求其输入在连接列(本例中为第 1 列)上排序。 sed 1d
命令在对数据进行排序之前删除标题行。
-e0
表示“当值丢失时输入 0
”。 -a1
和 -a2
选项表示“保留文件 1 和文件 2 中的所有行”。 -t $'\t'
选项使用 Bash 的 ANSI C Quoting为分隔符生成一个选项卡。如果省略 -t
选项,它可以“工作”,但输出列由空格而不是制表符分隔。 -o
选项指定要打印的列: 0
是连接列(每个文件中的第 1 列); 1.2
是文件 1 中的第 2 列,依此类推。第二个 join
中的文件名 -
表示“读取标准输入”。
样本数据的输出是:
James 0 0 65
Joey 54 23 0
Kyle 87 76 34
Marge 87 0 23
Pedro 0 76 76
Sia 43 0 65
<小时/>
这里有一些处理 10 个示例文件的代码。我也需要生成数据,因此我使用了工具包中的许多工具来完成此操作 - random
、perturb
、range
(与标准 seq
非常相似)和 shuffle
:
for sample in $(range -f '%03d' 1 10)
do
random -n 9 -T '%{ABCDEFJKMPS}s %[11:90]d %{BG}s' |
sort -u -k1,1 |
join -o 1.2,2.2,2.3 names - |
shuffle |
sed 's/ / /g' |
perturb -f '%2.0f' -p 10 -c 2 > "sample$sample.file"
done
随机数据生成器的一个小问题是它还不允许您从(多字符)名称列表中选择随机条目,因此我使用了缩写列表并将它们映射到带有 names
文件的名称。这有点奇怪,但您应该已经拥有数据并且不需要生成随机数据。文件 names
包含:
A Alex
B Belle
C Cynthia
D Doreen
E Elizabeth
F Ferdinand
J James
J Joey
K Kyle
M Marge
P Pedro
S Sia
例如,sample001.file
最终包含:
Belle 81 B
Marge 62 B
Ferdinand 37 B
Sia 44 B
Doreen 45 G
Elizabeth 18 G
Joey 16 B
James 19 B
然后,连接代码需要在进行任何连接之前生成所有名称的列表,否则您将看不到未出现在第一个示例文件中的名称的正确分数。这不使用任何非标准工具。
tmp=$(mktemp ./tmp.XXXXXX)
trap 'rm -f "$tmp" "$tmp".?; exit 1' 0 1 2 3 13 15
sed 's/[[:space:]].*//' "$@" | sort -u > $tmp.0
join_cmd()
{
join -e 0 -a 1 -a 2 -o "$outcols" "$@" > "$tmp.2"
}
outcols="0,2.2"
# Generate list of all names
join_cmd "$tmp.0" <(sort "$1")
mv "$tmp.2" "$tmp.1"
shift
outcols="0,1.2,2.2"
for sample in "$@"
do
join_cmd "$tmp.1" <(sort "$sample")
sed 's/[[:space:]]\([0-9][0-9]*\)$/,\1/' "$tmp.2" > "$tmp.1"
done
# Don't hard code the output file name — do that on the command line that
# invokes this script (same as you specify the input file names on the command line).
sed 's/,/ /g' "$tmp.1" # > integrate.file
rm -f "$tmp" "$tmp".?
trap 0 1 2 3 13 15
无需通过将数字映射到逗号分隔的列表来不断扩展连接列的列表,就可以解决这个问题。
$ column -t integrate.file
Alex 0 0 78 0 65 21 0 38 64 0
Belle 81 12 15 58 0 27 0 13 0 52
Cynthia 0 58 0 52 12 0 0 77 0 94
Doreen 45 49 0 85 0 0 57 32 81 63
Elizabeth 18 64 19 39 18 94 52 0 0 25
Ferdinand 37 0 0 0 0 64 72 21 0 28
James 19 0 0 77 0 48 78 59 39 23
Joey 16 0 0 79 0 48 78 70 39 19
Kyle 0 80 0 65 54 26 0 88 0 0
Marge 62 37 13 0 0 81 0 0 24 69
Pedro 0 0 40 0 47 74 79 0 0 0
Sia 44 0 27 0 55 0 43 0 32 0
$
您可以在输出的第 2 列中看到 sample000.file
的内容。您可以看到 names
中的所有名称都出现在输出中,并且每个示例文件都有一个编号。
关于linux - 在 Linux 中连接多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38494701/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!