I want to use "awk" or sed to print all the lines that start with "comm=" in a file

25,659

Solution 1

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

Solution 2

You could use grep also :

grep comm= filex

this will display all the lines containing comm=.

Solution 3

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.

Share:
25,659
wael
Author by

wael

Updated on July 09, 2022

Comments

  • wael
    wael almost 2 years

    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 ?

  • wael
    wael over 12 years
    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
  • Michael J. Barber
    Michael J. Barber over 12 years
    @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.
  • wael
    wael over 12 years
    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
  • potong
    potong over 12 years
    For brevity you might like sed '/\<comm=/!d'
  • vefthym
    vefthym about 10 years
    or grep "^comm=" filex to match lines starting with comm= ?