合并行时等效于PHP的终端命令爆破 [英] Terminal command equivalent of PHP implode when combining lines

查看:47
本文介绍了合并行时等效于PHP的终端命令爆破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几行,想将这行分成5条,然后将其内嵌到MySQL IN ()查询中.

I have a couple of lines and want to group the line into 5 and then implode it for MySQL IN () query.

直到现在我已经弄清楚了

I have made it out until this

awk '{ printf "%s", $0; if (NR % 5 == 0) print ""; else printf " " }

例如,我想要下面的这些行

For example, I want these lines below

1
2
3
4
5
6

成为

1,2,3,4,5
6

如果我使用这个

awk '{ printf "%s", $0; if (NR % 5 == 0) print ""; else printf "," }

然后,如果所有行均不能被5整除,则输出的尾随行将带有,

Then the output will have , in the trailing line if all the lines are not divisible by 5

更新

以前的标题是awk而不是bash,但是事实证明,有比awk更简单的解决方案.我的目标是做这样的事情

Previous title is awk instead of bash, but turns out there is more simpler solution than awk. My goal is to do something like this

$seq 12 | pr -7ats, | xargs -I X echo "SELECT * FROM Table IN (X)" #or execute mysql
SELECT * FROM Table WHERE id IN (1,2,3,4,5,6,7)
SELECT * FROM Table WHERE id IN (8,9,10,11,12)

推荐答案

pr是用于此的工具

$ seq 6 | pr -5ats,
1,2,3,4,5
6

$ seq 18 | pr -5ats, 
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18

这篇关于合并行时等效于PHP的终端命令爆破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆