shell - 想要在shell脚本中解析一个字符串

标签 shell

我有一个格式为“foo=”是第一个” foo1=“是第二个” foo3=“是第三个”的字符串以及许多类似于上述模式的字段。我想解析这个并将 o/p 作为

foo              foo1              foo3
is a first one   is a second one   is a third one

非常感谢任何帮助。

最佳答案

对输出进行列化是困难的部分。 (我同意 Noufal 的观点,即 perl 是一个很好的方法。)但是,使用其他更基本的工具也是可行的。例如:

$ cat input
foo
is a first one
foo1
is a second one
foo3
is a third one
$ ( awk 'NR % 2' input; awk 'NR % 2 == 0' input ) | paste - - - | column -s'   ' -t
foo             foo1             foo3
is a first one  is a second one  is a third one

(请注意,列的 -s 参数应在单引号之间包含一个制表符。)

关于shell - 想要在shell脚本中解析一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5786393/

相关文章:

linux - 如何使用 read 命令仅读取字符串的某些部分?

linux - 用另一个文件中的值替换文件中的字符串

python - 在Paramiko中执行多个命令,以便命令受到其前任命令的影响

bash - 将 wc 行附加到文件名

python - 运行 Python 脚本后在给定目录中的 cd 终端?

linux - Shell 脚本 - 两个 for 循环和更改文件扩展名

python - 向 DNS 查询时欺骗 src ip 地址

linux - 动态替换字符串的shell脚本

shell - 使 Emacs eshell/echo 换行符在常规终端中表现得像

linux -/usr/bin/时间和多核平台中的时间