python - 将两行中的值附加到 key 对中

标签 python perl bash

我的文本文件输出看起来像这样,分为两行:

DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|rca

我想要的输出太像这样:

DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca

我没有运气尝试这个:

sed '$!N;s/|/\n/' foo

欢迎任何建议,谢谢。

最佳答案

由于您只有两行,这可能是一种方法:

$ paste -d' ' <(head -1 file | sed 's/|/\n/g') <(tail -1 file | sed 's/|/\n/g')
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca

按件。让我们获取第一行并将每个管道 | 替换为新行:

$ head -1 file | sed 's/|/\n/g'
DelayTimeThreshold
MaxDelayPerMinute
Name

对最后一行执行相同的操作:

$ tail -1 file | sed 's/|/\n/g'
10000
5
rca

然后只需粘贴两个结果并用空格作为分隔符即可:

paste -d' ' output1 output2

关于python - 将两行中的值附加到 key 对中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19911316/

相关文章:

multithreading - Perl线程: Why does the output come by order and not mixed-up?

bash - 函数局部只读与全局只读同名变量

BASH 万一

python ascii码到utf

python - 从数据库传递 URL 参数

perl - 运行 Inline::C 时出错

python - 将 perl soap 应用程序移植到 python

linux - 将 .txt 文件的内容分类为 .bash_profile

python - Python:将数据框转换为列表,列表中包含字符串项

python 服务器-客户端 nat 遍历