使用 awk 进行文件格式化的一个简单但符合逻辑的问题
我对这些 awk 和 shell 很陌生,并且遇到了一个简单但合乎逻辑的问题。.
输入文件:
6000 9876 5675 ....
8576 8765 9845 ...
....
输出文件:(必需)
60 00 98 76 56 75 ....
85 76 87 65 98 45 ...
....
将输出转换为输入是一个相当麻烦的事情 。简单的任务
awk '{printf("%s%s %s%s %s%s %s%s", $1, $2, $3, $4, $5, $6, $7, $8)}' output_file
> input_file
但是将输入转换为输出我无法猜测
(而且字段的数量事先也不知道,尽管我认为 NF 的一些逻辑可以解决这个问题)但主要问题是即使我知道不。那么如何继续呢?
最小。要读取的单元将类似于 $1,$2
等。我需要打破它们并需要在它们之间插入一个空格。
我对 regex 不太了解 但尝试一下。也许使用 sed
和 regex
进行一些操作可以帮助我。
请提供您的宝贵建议。`
I am new to these awk and shell things and got stuck with a simple but logical issue ..
Input File:
6000 9876 5675 ....
8576 8765 9845 ...
....
Output File: (required)
60 00 98 76 56 75 ....
85 76 87 65 98 45 ...
....
Converting output to input is a rather easy task
awk '{printf("%s%s %s%s %s%s %s%s", $1, $2, $3, $4, $5, $6, $7, $8)}' output_file
> input_file
But converting input to output I am getting no guess
(also no. of fields are not known in advance although I think some logic with NF
can solve this issue) but main problem is even if I know no. of fileds then how to proceed for that??
The min. unit to be read wll be like $1,$2
etc.. and I need to break them and need to insert a space between them.
I don't know much about regex
but trying my hand on it.May be some manipulation with sed
and regex
could help me out.
Please provide your valuable suggestons.`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个 sed 示例:
Here's a sed example:
或者避免第一个 tr:
Alternatively to avoid the first tr:
由于问题标题提到“awk”,我给出了 awk 解决方案,尽管已经接受了答案:
这需要 gawk:
since the question title mentioned "awk", I gave an awk solution though there is already accepted answer:
this needs gawk: