Home > Net >  sed: Getting Two Extra Characters in the Results
sed: Getting Two Extra Characters in the Results

Time:10-16

How can I display the next two characters from sed results (wildcard characters and then stop the results)?

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

Expecting test

Actual results te.. line

CodePudding user response:

You can use

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

See the online demo. Here,

  • -n - suppresses the default line output
  • .*\(te..\).* - matches any zero or more chars, then captured into Group 1 te and any two chars, and then matches the rest of the string
  • \1 - replaces the whole match with the value of Group 1
  • p - only prints the result of the substitution.
  • Related