Home > Net >  print all lines with a specific char in last word using sed
print all lines with a specific char in last word using sed

Time:11-07

Using sed how can I print all lines in a file that has the letter a in the last word?

I tried something like this sed -n -E '/[ e]./w\./p' < 'textfile'

But Im very new to sed so not really understanding it.

CodePudding user response:

You might use

sed -nE '/[^a[:space:]]*a[^[:space:]]*[[:space:]]*$/p' textfile

Here:

  • -n disable the default printing of the line
  • -E extended regular expressions
  • /p print the addressed line

The pattern matches:

  • [^a[:space:]]* Optionally match non whitespace characters except for the a char
  • a Match the a char
  • [^[:space:]]* Match optional non whitespace chars
  • [[:space:]]* Match optional spaces
  • $ End of string

Example input

this is a test
a
atest
testa
testatest
atest this is
testa this is
testatest this is
this is a test$%a
a word with special chars !@#$%a!@#$%
a word with special chars !@#$%a!@#$% bcd

Output

a
atest
testa
testatest
this is a test$%a
a word with special chars !@#$%a!@#$%
  • Related