11

I want to use "awk" or "sed" to print all the lines that start with comm= from the file filex, Note that each line contains "comm=somthing"

for example : comm=rm , comm=ll, comm=ls  ....

How can i achieve that ?

2
  • How is made your file ? one comm=rm per line ? Nov 30, 2011 at 13:40
  • yes per line, each line contains comm=x
    – wael
    Nov 30, 2011 at 13:43

4 Answers 4

26

For lines that start with comm=

sed -n '/^comm=/p' filex

awk '/^comm=/' filex

If comm= is anywhere in the line then

sed -n '/comm=/p' filex

awk '/comm=/' filex
3
  • that only works it comm is at the beginning of the line, how to make it work if comm is at any place in each line, thanks for ur help Jaypa
    – wael
    Nov 30, 2011 at 13:45
  • this is returning the whole line i just want to return the match, and this is how it can be done : grep -o '\<comm=[[:alnum:]]*\>' , thanks for your help
    – wael
    Nov 30, 2011 at 13:58
  • For brevity you might like sed '/\<comm=/!d'
    – potong
    Nov 30, 2011 at 17:18
6

You could use grep also :

grep comm= filex

this will display all the lines containing comm=.

1
  • 1
    or grep "^comm=" filex to match lines starting with comm= ?
    – vefthym
    May 20, 2014 at 14:33
2

Here's an approach using grep:

grep -o '\<comm=[[:alnum:]]*\>'

This treats a word as consisting of alphanumeric characters; extend the character class as needed.

1
  • 2
    @bob Given that you've stated in a comment that it's just one comm= per line, this solution is overkill; use the answer from Cédric Julien. Additionally, to say "thanks" on Stackoverflow, you can just upvote the useful answers rather than commenting. Nov 30, 2011 at 13:54
0

If grep is ok to use, you could give a try to:

grep -E "^comm=" file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.