Search code examples
ack

Perl ack tool with output parameter and new lines


I use ack very often, but something I have not been able to discover is how to make it add new lines when using the output parameter.

Example

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

I expect

User name: Joe
User ID: 123

but I get

User name: Joe\nUser ID: 123

Anyway to make ack respect the new lines characters in the output?

Thanks


Solution

  • You are passing \n to ack instead of a newline. Replace

    --output='User name: $1\nUser ID: $2'
    

    with

    --output='User name: $1
    User ID: $2'
    

    or

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

    or

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