shell脚本读取并打印字符串的一部分
大家好,
我有一个输入文件,其中有几行数字(接近 2000 行),我想将数字字符串的每一行的“从右边数第二到八位数字”提取到一个单独的文件中,其中结果用逗号分隔,如图所示。
Example: input.txt
00000000000001303275310752
00000000000001827380519015
00000000000000800081610361
00000000000000449481894004
00000000000000449481894004
00000000000001812612607514
Expected result: newfile.txt
7531075,
8051901,
8161036,
8189400,
8189400,
1260751,
I'm guessing something like 'sed' can be used to solve my problem, but i'm不太确定如何实现这一目标。我已连接到运行 Solaris 5.10 的计算机。如果有人能通过简短的解释来指导我,我将不胜感激。
问候,
新手。
Good day members,
I have an input file which has rows of numerical digits ( close to 2000 rows ) I want to extract out " the second to the eight digit from the right" of every row of the numeric string into a separate file, with the result separated by a comma as shown.
Example: input.txt
00000000000001303275310752
00000000000001827380519015
00000000000000800081610361
00000000000000449481894004
00000000000000449481894004
00000000000001812612607514
Expected result: newfile.txt
7531075,
8051901,
8161036,
8189400,
8189400,
1260751,
I'm guessing something like 'sed' can be used to solve my problem, but i'm not quite sure how to go about achieving this. I'm connected to a machine running on Solaris 5.10 Appreciate if someone can guide me with a brief explanation.
regards,
novice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于定宽输入,可以尝试:
也就是说,提取输入txt的第19到26个字符,然后用逗号替换行尾。
如果你有可变长度的线,你将需要一些不同的东西。
For fixed width input, try:
which is to say, extract the 19th to 26th character of input txt and then replace the end of line with a comma.
If you have variable length lines, you will need something a little different.
您可以使用以下方法截断前导零:
因此,类似:
应该有效。
You can truncate the leading zeros with:
Thus something like:
should work.
尝试:
或者如果您不喜欢正则表达式:
这适用于任何固定或可变长度的行。
Try:
Or if you don't like regular expressions:
This will work for any fixed or variable length line.
这是 python 的解决方案,应该很直观:
Here is a solution in python, it should be intuitive: