带有输出参数和新行的 Perl ack 工具

Perl ack tool with output parameter and new lines

我经常使用ack,但是我没能发现的是如何让它在使用输出参数时添加新行。

例子

echo 'name=Joe,id=123' | ack '^name=(\w+),id=(\d+)' --output="User name: \nUser ID: "

我希望

User name: Joe
User ID: 123

但我明白了

User name: Joe\nUser ID: 123

如何使 ack 尊重输出中的新行字符?

谢谢

您可以通过 @{[ … ]} 插值将任意代码放入该字符串。出于某种原因,文字 "\n" 字符串将不起作用,但 chr 0x0A$/ 可以。

示例:

$ echo a b | ack '(\w+)\s+(\w+)' --output '@{[ chr 0x0A ]}'

输出:

a
b

请注意,此类功能将来可能会中断,另请参阅 https://github.com/beyondgrep/ack2/issues/531

等讨论

您将 \n 传递给 ack 而不是换行符。替换

--output='User name: \nUser ID: '

--output='User name: 
User ID: '

--output="$( printf 'User name: \nUser ID: ' )"

--output=$'User name: \nUser ID: '                   # bash