根据第一个字段对内容进行排序并将第二个字段输出到新文件中
我有一个像这样的文件:
3 EOE
5 APPLE
6 NOBODY
我需要解析它并在unix提示符下将第一列中带有“3”的所有内容输出到filename.3,将“4”输出到filename.4等...
I've got a file which is like this:
3 EOE
5 APPLE
6 NOBODY
i need to parse this and output all with '3' in the first column into filename.3, '4' into filename.4, etc... from the unix prompt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西应该可以工作(我还没有测试过):
read
将读取一行文本,然后将其在空白处分割成“单词”,就像运行命令时一样。它将把第一个“word”分配给第一个变量名称(在本例中为num
- 将获取数字),将第二个“word”分配给第二个变量名称(rest
)代码>),等等。如果变量用完,它将将该行的剩余部分附加到最后一个变量(此处为rest
)。当 read 成功处理一行时,它返回零,这在 shell 脚本中是“成功”,因此 while 循环将继续执行,读取后续行。当
read
到达文件末尾时,它将返回1,停止while
循环。Something like this should work (I haven't tested it):
read
will read a line of text, then split it up on whitespace into "words", like when you run a command. It will assign the first "word" to the first variable name (num
in this case - which will get the number), the second "word" to the second variable name (rest
), and so on. If it runs out of variables, it will append the remainder of the line to the last variable (rest
, here).When
read
processes a line successfully it returns zero, which is "success" in shell scripting, so thewhile
loop will keep going, reading subsequent lines. Whenread
hits the end of the file, it will return 1, stopping thewhile
loop.如果您想要整行:
If you want the entire line:
使用 bash for 循环、cut 和 grep
using bash for loops, cut and grep