Linux – sed: Get two extra characters in the result

sed: Get two extra characters in the result… here is a solution to the problem.

sed: Get two extra characters in the result

How do I display the next two characters in the sed result (wildcard character and then stop result)?

echo 'this is a test line' | sed 's/^.*te*/te.. /'

Expect.
Test

Actual results te: line

Solution

You can use

it

sed -n 's/.*\(te.. \).*/\1/p' <<< 'this is a test line'

See online demo

  • -n – Suppresses default row output
  • .*\(te.. \).* – Matches any zero or more characters, then captures to set 1 te and any two characters, and then matches the rest of the string
  • \1 – Replaces the entire match with the value of Group 1
  • p – Print only the results of the replacement.

Related Problems and Solutions