Bash find & replace using regex pipe

Content

Bash Find & Replace

Find & replace using regex on the command line using perl.

perl -pe "s/find/replace/g"

Parentheses for grouping and capturing is also supported.

$ echo "myfile.txt" | perl -pe "s/(.*)\.txt/\1.log/" myfile.log $ find=".*(\d{4})\/(\d{2})\/(\d{2})\/.*" $ replace="year: \1; month: \2; day: \3" $ echo "https://www.example.com/2014/04/28/page-slug" | perl -pe "s/${find}/${replace}/" year: 2014; month: 04; day: 28
Posted . Did this help you? Please let me know with a comment. Thank you

Comments (RSS)

  1. anonymous

    For group replacements, use $1, $2, etc.

    perl -pe 's/(\d+).*/\1/g'