awk:多行输出单个字符串,易于替换
000 000 000 000 (4 个字段,每个字段由 3 个零组成,由空格分隔)
生成 4 个新行的过程
100 000 000 000
000 100 000 000 000
000 100 000
000 000 000 100
每行一组三个零被替换为 100
我该怎么做?
汤姆
000 000 000 000 (4 fields each of which is a group of 3 zeros separated by a space)
Process to generate 4 new lines
100 000 000 000
000 100 000 000
000 000 100 000
000 000 000 100
On ea line a group of three zeros is replaced by 100
How can I do this ?
tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
使用
for
循环对字段进行迭代。每个字段($i
- 字段号i
)都保存到临时变量f
中。然后该字段的内容被替换。打印记录 ($0
)。使用临时值将该字段返回到其先前的值。如果使用以下数据,可能会更容易理解:
001 002 003 004
。那么输出将如下所示:Here's a shell script version using sed:
or
or Bash without any external utility:
或者许多其他可能性。
Edit:
The fields are iterated over using the
for
loop. Each field ($i
- for the field numberi
) is saved to a temporary variablef
. Then the contents of the field are replaced. The record ($0
) is printed. The field is returned to its previous value using the temporary value.It might be easier to follow if this data was used:
001 002 003 004
. Then the output would look like:Here's a shell script version using
sed
:or
or Bash without any external utilities:
Or many other possibilities.